Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
3
Task/Sort-stability/00-META.yaml
Normal file
3
Task/Sort-stability/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Sort_stability
|
||||
note: Sorting Algorithms
|
||||
29
Task/Sort-stability/00-TASK.txt
Normal file
29
Task/Sort-stability/00-TASK.txt
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{{Sorting Algorithm}}
|
||||
[[Category:Sorting]]
|
||||
|
||||
When sorting records in a table by a particular column or field, a [[wp:Stable_sort#Stability|stable sort]] will always retain the relative order of records that have the same key.
|
||||
|
||||
|
||||
;Example:
|
||||
In this table of countries and cities, a stable sort on the ''second'' column, the cities, would keep the '''US Birmingham''' above the '''UK Birmingham'''.
|
||||
|
||||
(Although an unstable sort ''might'', in this case, place the '''US Birmingham''' above the '''UK Birmingham''', a stable sort routine would ''guarantee'' it).
|
||||
|
||||
<pre>
|
||||
UK London
|
||||
US New York
|
||||
US Birmingham
|
||||
UK Birmingham
|
||||
</pre>
|
||||
|
||||
Similarly, stable sorting on just the first column would generate '''UK London''' as the first item and '''US Birmingham''' as the last item (since the order of the elements having the same first word – '''UK''' or '''US''' – would be maintained).
|
||||
|
||||
|
||||
;Task:
|
||||
:# Examine the documentation on any in-built sort routines supplied by a language.
|
||||
:# Indicate if an in-built routine is supplied
|
||||
:# If supplied, indicate whether or not the in-built routine is stable.
|
||||
|
||||
<br>
|
||||
(This [[wp:Stable_sort#Comparison_of_algorithms|Wikipedia table]] shows the stability of some common sort routines).
|
||||
<br><br>
|
||||
293
Task/Sort-stability/AArch64-Assembly/sort-stability.aarch64
Normal file
293
Task/Sort-stability/AArch64-Assembly/sort-stability.aarch64
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program stableSort641.s */
|
||||
|
||||
/* use merge sort and pointer table */
|
||||
/* but use a extra table of pointer for the merge */
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
/*******************************************/
|
||||
/* Structures */
|
||||
/********************************************/
|
||||
/* city structure */
|
||||
.struct 0
|
||||
city_name: //
|
||||
.struct city_name + 8 // string pointer
|
||||
city_country: //
|
||||
.struct city_country + 8 // string pointer
|
||||
city_end:
|
||||
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
sMessResult: .asciz "Name : @ country : @ \n"
|
||||
szMessSortName: .asciz "Sort table for name of city :\n"
|
||||
szMessSortCountry: .asciz "Sort table for country : \n"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
// cities name
|
||||
szLondon: .asciz "London"
|
||||
szNewyork: .asciz "New York"
|
||||
szBirmin: .asciz "Birmingham"
|
||||
szParis: .asciz "Paris"
|
||||
// country name
|
||||
szUK: .asciz "UK"
|
||||
szUS: .asciz "US"
|
||||
szFR: .asciz "FR"
|
||||
.align 4
|
||||
TableCities:
|
||||
e1: .quad szLondon // address name string
|
||||
.quad szUK // address country string
|
||||
e2: .quad szParis
|
||||
.quad szFR
|
||||
e3: .quad szNewyork
|
||||
.quad szUS
|
||||
e4: .quad szBirmin
|
||||
.quad szUK
|
||||
e5: .quad szParis
|
||||
.quad szUS
|
||||
e6: .quad szBirmin
|
||||
.quad szUS
|
||||
/* pointers table */
|
||||
ptrTableCities: .quad e1
|
||||
.quad e2
|
||||
.quad e3
|
||||
.quad e4
|
||||
.quad e5
|
||||
.quad e6
|
||||
.equ NBELEMENTS, (. - ptrTableCities) / 8
|
||||
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
sZoneConv: .skip 24
|
||||
ptrTableExtraSort: .skip 8 * NBELEMENTS
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
ldr x0,qAdrptrTableCities // address pointers table
|
||||
bl displayTable
|
||||
|
||||
ldr x0,qAdrszMessSortName
|
||||
bl affichageMess
|
||||
|
||||
ldr x0,qAdrptrTableCities // address pointers table
|
||||
mov x1,0 // not use in routine
|
||||
mov x2,NBELEMENTS - 1 // number of élements
|
||||
mov x3,#city_name // sort by city name
|
||||
mov x4,#'A' // alphanumeric
|
||||
ldr x5,qAdrptrTableExtraSort
|
||||
bl mergeSort
|
||||
ldr x0,qAdrptrTableCities // address table
|
||||
bl displayTable
|
||||
|
||||
ldr x0,qAdrszMessSortCountry
|
||||
bl affichageMess
|
||||
|
||||
ldr x0,qAdrptrTableCities // address table
|
||||
mov x1,0 // not use in routine
|
||||
mov x2,NBELEMENTS - 1 // number of élements
|
||||
mov x3,#city_country // sort by city country
|
||||
mov x4,#'A' // alphanumeric
|
||||
ldr x5,qAdrptrTableExtraSort
|
||||
bl mergeSort
|
||||
ldr x0,qAdrptrTableCities // address table
|
||||
bl displayTable
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0,0 // return code
|
||||
mov x8,EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
|
||||
qAdrsZoneConv: .quad sZoneConv
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
qAdrsMessResult: .quad sMessResult
|
||||
qAdrTableCities: .quad TableCities
|
||||
qAdrszMessSortName: .quad szMessSortName
|
||||
qAdrptrTableExtraSort: .quad ptrTableExtraSort
|
||||
qAdrszMessSortCountry: .quad szMessSortCountry
|
||||
qAdrptrTableCities: .quad ptrTableCities
|
||||
/******************************************************************/
|
||||
/* merge sort */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of table */
|
||||
/* x1 contains the index of first element */
|
||||
/* x2 contains the number of element */
|
||||
/* x3 contains the offset of area sort */
|
||||
/* x4 contains the type of area sort N numeric A alpha */
|
||||
/* x5 contains address extra area */
|
||||
mergeSort:
|
||||
stp x3,lr,[sp,-16]! // save registers
|
||||
stp x4,x5,[sp,-16]! // save registers
|
||||
stp x6,x7,[sp,-16]! // save registers
|
||||
stp x8,x9,[sp,-16]! // save registers
|
||||
stp x10,x11,[sp,-16]! // save registers
|
||||
mov x6,x1 // save index first element
|
||||
mov x7,x2 // save number of element
|
||||
mov x11,x0 // save address table
|
||||
cmp x2,x1 // end ?
|
||||
ble 100f
|
||||
add x9,x2,x1
|
||||
lsr x9,x9,1 // number of element of each subset
|
||||
mov x2,x9
|
||||
bl mergeSort
|
||||
mov x1,x9 // restaur number of element of each subset
|
||||
add x1,x1,1
|
||||
mov x2,x7 // restaur number of element
|
||||
bl mergeSort // sort first subset
|
||||
add x10,x9,1
|
||||
1:
|
||||
sub x1,x10,1
|
||||
sub x8,x10,1
|
||||
ldr x2,[x0,x1,lsl 3]
|
||||
str x2,[x5,x8,lsl 3]
|
||||
sub x10,x10,1
|
||||
cmp x10,x6
|
||||
bgt 1b
|
||||
mov x10,x9
|
||||
2:
|
||||
add x1,x10,1
|
||||
add x8,x7,x9
|
||||
sub x8,x8,x10
|
||||
ldr x2,[x0,x1,lsl 3]
|
||||
str x2,[x5,x8,lsl 3]
|
||||
add x10,x10,1
|
||||
cmp x10,x7
|
||||
blt 2b
|
||||
|
||||
mov x10,x6 //k
|
||||
mov x1,x6 // i
|
||||
mov x2,x7 // j
|
||||
3:
|
||||
mov x0,x5 // table address x1 = i x2 = j x3 = area sort offeset
|
||||
bl comparArea
|
||||
cmp x0,0
|
||||
bgt 5f
|
||||
blt 4f
|
||||
// if equal and i < pivot
|
||||
cmp x1,x9
|
||||
ble 4f // inverse to stable
|
||||
b 5f
|
||||
4: // store element subset 1
|
||||
mov x0,x5
|
||||
ldr x6,[x5,x1, lsl 3]
|
||||
str x6,[x11,x10, lsl 3]
|
||||
add x1,x1,1
|
||||
b 6f
|
||||
5: // store element subset 2
|
||||
mov x0,x5
|
||||
ldr x6,[x5,x2, lsl 3]
|
||||
str x6,[x11,x10, lsl 3]
|
||||
sub x2,x2,1
|
||||
6:
|
||||
add x10,x10,1
|
||||
cmp x10,x7
|
||||
ble 3b
|
||||
mov x0,x11
|
||||
|
||||
100:
|
||||
ldp x10,x11,[sp],16 // restaur 2 registers
|
||||
ldp x8,x9,[sp],16 // restaur 2 registers
|
||||
ldp x6,x7,[sp],16 // restaur 2 registers
|
||||
ldp x4,x5,[sp],16 // restaur 2 registers
|
||||
ldp x3,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
/******************************************************************/
|
||||
/* comparison sort area */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of table */
|
||||
/* x1 indice area sort 1 */
|
||||
/* x2 indice area sort 2 */
|
||||
/* x3 contains the offset of area sort */
|
||||
/* x4 contains the type of area sort N numeric A alpha */
|
||||
comparArea:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
stp x4,x5,[sp,-16]! // save registers
|
||||
stp x6,x7,[sp,-16]! // save registers
|
||||
stp x8,x9,[sp,-16]! // save registers
|
||||
|
||||
ldr x1,[x0,x1,lsl 3] // load pointer element 1
|
||||
ldr x6,[x1,x3] // load area sort element 1
|
||||
ldr x2,[x0,x2,lsl 3] // load pointer element 2
|
||||
ldr x7,[x2,x3] // load area sort element 2
|
||||
cmp x4,'A' // numeric or alpha ?
|
||||
beq 1f
|
||||
cmp x6,x7 // compare numeric value
|
||||
blt 10f
|
||||
bgt 11f
|
||||
b 12f
|
||||
1: // else compar alpha string
|
||||
mov x8,#0
|
||||
2:
|
||||
ldrb w9,[x6,x8] // byte string 1
|
||||
ldrb w5,[x7,x8] // byte string 2
|
||||
cmp w9,w5
|
||||
bgt 11f
|
||||
blt 10f
|
||||
|
||||
cmp w9,#0 // end string 1
|
||||
beq 12f // end comparaison
|
||||
add x8,x8,#1 // else add 1 in counter
|
||||
b 2b // and loop
|
||||
|
||||
10: // lower
|
||||
mov x0,-1
|
||||
b 100f
|
||||
11: // highter
|
||||
mov x0,1
|
||||
b 100f
|
||||
12: // equal
|
||||
mov x0,0
|
||||
100:
|
||||
ldp x8,x9,[sp],16 // restaur 2 registers
|
||||
ldp x6,x7,[sp],16 // restaur 2 registers
|
||||
ldp x4,x5,[sp],16 // restaur 2 registers
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
|
||||
/******************************************************************/
|
||||
/* Display table elements */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of table */
|
||||
displayTable:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
stp x4,x5,[sp,-16]! // save registers
|
||||
stp x6,x7,[sp,-16]! // save registers
|
||||
mov x2,x0 // table address
|
||||
mov x3,0
|
||||
1: // loop display table
|
||||
lsl x4,x3,#3 // offset element
|
||||
ldr x6,[x2,x4] // load pointer
|
||||
ldr x1,[x6,city_name]
|
||||
ldr x0,qAdrsMessResult
|
||||
bl strInsertAtCharInc // put name in message
|
||||
ldr x1,[x6,city_country] // and put country in the message
|
||||
bl strInsertAtCharInc // insert result at @ character
|
||||
bl affichageMess // display message
|
||||
add x3,x3,1
|
||||
cmp x3,#NBELEMENTS
|
||||
blt 1b
|
||||
ldr x0,qAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
100:
|
||||
ldp x6,x7,[sp],16 // restaur 2 registers
|
||||
ldp x4,x5,[sp],16 // restaur 2 registers
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
27
Task/Sort-stability/AWK/sort-stability.awk
Normal file
27
Task/Sort-stability/AWK/sort-stability.awk
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# syntax: GAWK -f SORT_STABILITY.AWK [-v width=x] -v field=x SORT_STABILITY.TXT
|
||||
#
|
||||
# sort by country: GAWK -f SORT_STABILITY.AWK -v field=1 SORT_STABILITY.TXT
|
||||
# sort by city: GAWK -f SORT_STABILITY.AWK -v field=2 SORT_STABILITY.TXT
|
||||
#
|
||||
# awk sort is not stable. Stability may be achieved by appending the
|
||||
# record number, I.E. NR, to each key.
|
||||
#
|
||||
BEGIN {
|
||||
FIELDWIDTHS = "4 20" # 2 fields: country city
|
||||
PROCINFO["sorted_in"] = "@ind_str_asc"
|
||||
if (width == "") {
|
||||
width = 6
|
||||
}
|
||||
}
|
||||
{ arr[$field sprintf("%0*d",width,NR)] = $0 }
|
||||
END {
|
||||
if (length(NR) > width) {
|
||||
printf("error: sort may still be unstable; change width to %d\n",length(NR))
|
||||
exit(1)
|
||||
}
|
||||
printf("after sorting on field %d:\n",field)
|
||||
for (i in arr) {
|
||||
printf("%s\n",arr[i])
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
10
Task/Sort-stability/AppleScript/sort-stability-1.applescript
Normal file
10
Task/Sort-stability/AppleScript/sort-stability-1.applescript
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
set aTable to "UK London
|
||||
US New York
|
||||
US Birmingham
|
||||
UK Birmingham"
|
||||
|
||||
-- -s = stable sort; -t sets the field separator, -k sets the sort "column" range in field numbers.
|
||||
set stableSortedOnColumn2 to (do shell script ("sort -st'" & tab & "' -k2,2 <<<" & quoted form of aTable))
|
||||
set stableSortedOnColumn1 to (do shell script ("sort -st'" & tab & "' -k1,1 <<<" & quoted form of aTable))
|
||||
return "Stable sorted on column 2:" & (linefeed & stableSortedOnColumn2) & (linefeed & linefeed & ¬
|
||||
"Stable sorted on column 1:") & (linefeed & stableSortedOnColumn1)
|
||||
11
Task/Sort-stability/AppleScript/sort-stability-2.applescript
Normal file
11
Task/Sort-stability/AppleScript/sort-stability-2.applescript
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
"Stable sorted on column 2:
|
||||
US Birmingham
|
||||
UK Birmingham
|
||||
UK London
|
||||
US New York
|
||||
|
||||
Stable sorted on column 1:
|
||||
UK London
|
||||
UK Birmingham
|
||||
US New York
|
||||
US Birmingham"
|
||||
15
Task/Sort-stability/Arturo/sort-stability.arturo
Normal file
15
Task/Sort-stability/Arturo/sort-stability.arturo
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
records: @[
|
||||
#[country: "UK", city: "London"]
|
||||
#[country: "US", city: "New York"]
|
||||
#[country: "US", city: "Birmingham"]
|
||||
#[country: "UK", city: "Birmingham"]
|
||||
]
|
||||
|
||||
print "Original order:"
|
||||
loop records => print
|
||||
|
||||
print "\nSorted by country name:"
|
||||
loop sort.by:'country records => print
|
||||
|
||||
print "\nSorted by city name:"
|
||||
loop sort.by:'city records => print
|
||||
40
Task/Sort-stability/AutoHotkey/sort-stability.ahk
Normal file
40
Task/Sort-stability/AutoHotkey/sort-stability.ahk
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
Table =
|
||||
(
|
||||
UK, London
|
||||
US, New York
|
||||
US, Birmingham
|
||||
UK, Birmingham
|
||||
)
|
||||
|
||||
Gui, Margin, 6
|
||||
Gui, -MinimizeBox
|
||||
Gui, Add, ListView, r5 w260 Grid, Orig.Position|Country|City
|
||||
Loop, Parse, Table, `n, `r
|
||||
{
|
||||
StringSplit, out, A_LoopField, `,, %A_Space%
|
||||
LV_Add("", A_Index, out1, out2)
|
||||
}
|
||||
LV_ModifyCol(1, "77 Center")
|
||||
LV_ModifyCol(2, "100 Center")
|
||||
LV_ModifyCol(3, 79)
|
||||
Gui, Add, Button, w80, Restore Order
|
||||
Gui, Add, Button, x+10 wp, Sort Countries
|
||||
Gui, Add, Button, x+10 wp, Sort Cities
|
||||
Gui, Show,, Sort stability
|
||||
Return
|
||||
|
||||
GuiClose:
|
||||
GuiEscape:
|
||||
ExitApp
|
||||
|
||||
ButtonRestoreOrder:
|
||||
LV_ModifyCol(1, "Sort")
|
||||
Return
|
||||
|
||||
ButtonSortCountries:
|
||||
LV_ModifyCol(2, "Sort")
|
||||
Return
|
||||
|
||||
ButtonSortCities:
|
||||
LV_ModifyCol(3, "Sort")
|
||||
Return
|
||||
9
Task/Sort-stability/Elixir/sort-stability-1.elixir
Normal file
9
Task/Sort-stability/Elixir/sort-stability-1.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
cities = [ {"UK", "London"},
|
||||
{"US", "New York"},
|
||||
{"US", "Birmingham"},
|
||||
{"UK", "Birmingham"} ]
|
||||
|
||||
IO.inspect Enum.sort(cities)
|
||||
IO.inspect Enum.sort(cities, fn a,b -> elem(a,0) >= elem(b,0) end)
|
||||
IO.inspect Enum.sort_by(cities, fn {country, _city} -> country end)
|
||||
IO.inspect Enum.sort_by(cities, fn {_country, city} -> city end)
|
||||
1
Task/Sort-stability/Elixir/sort-stability-2.elixir
Normal file
1
Task/Sort-stability/Elixir/sort-stability-2.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
IO.inspect Enum.sort(cities, fn a,b -> elem(a,0) > elem(b,0) end)
|
||||
17
Task/Sort-stability/GAP/sort-stability.gap
Normal file
17
Task/Sort-stability/GAP/sort-stability.gap
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# According to section 21.18 of the reference manual, Sort is not stable (it's a Shell sort).
|
||||
# However, SortingPerm is stable. We will see it on an example, showing indexes of elements after the sort.
|
||||
|
||||
n := 20;
|
||||
L := List([1 .. n], i -> Random("AB"));
|
||||
# "AABABBBABBABAABABBAB"
|
||||
|
||||
|
||||
p := SortingPerm(L);
|
||||
# (3,10,15,17,18,19,9,14,7,13,6,12,16,8,4)(5,11)
|
||||
|
||||
a := Permuted(L, p);;
|
||||
b := Permuted([1 .. n], p);;
|
||||
|
||||
PrintArray(TransposedMat(List([1 .. n], i -> [a[i], b[i]])));
|
||||
# [ [ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B' ],
|
||||
# [ 1, 2, 4, 8, 11, 13, 14, 16, 19, 3, 5, 6, 7, 9, 10, 12, 15, 17, 18, 20 ] ]
|
||||
10
Task/Sort-stability/Groovy/sort-stability.groovy
Normal file
10
Task/Sort-stability/Groovy/sort-stability.groovy
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def cityList = ['UK London', 'US New York', 'US Birmingham', 'UK Birmingham',].asImmutable()
|
||||
[
|
||||
'Sort by city': { city -> city[4..-1] },
|
||||
'Sort by country': { city -> city[0..3] },
|
||||
].each{ String label, Closure orderBy ->
|
||||
println "\n\nBefore ${label}"
|
||||
cityList.each { println it }
|
||||
println "\nAfter ${label}"
|
||||
cityList.sort(false, orderBy).each{ println it }
|
||||
}
|
||||
47
Task/Sort-stability/Java/sort-stability.java
Normal file
47
Task/Sort-stability/Java/sort-stability.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class RJSortStability {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] cityList = { "UK London", "US New York", "US Birmingham", "UK Birmingham", };
|
||||
|
||||
String[] cn = cityList.clone();
|
||||
System.out.println("\nBefore sort:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
// sort by city
|
||||
Arrays.sort(cn, new Comparator<String>() {
|
||||
public int compare(String lft, String rgt) {
|
||||
return lft.substring(4).compareTo(rgt.substring(4));
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("\nAfter sort on city:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
cn = cityList.clone();
|
||||
System.out.println("\nBefore sort:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
// sort by country
|
||||
Arrays.sort(cn, new Comparator<String>() {
|
||||
public int compare(String lft, String rgt) {
|
||||
return lft.substring(0, 2).compareTo(rgt.substring(0, 2));
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("\nAfter sort on country:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
7
Task/Sort-stability/JavaScript/sort-stability.js
Normal file
7
Task/Sort-stability/JavaScript/sort-stability.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
ary = [["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]
|
||||
print(ary);
|
||||
|
||||
ary.sort(function(a,b){return (a[1]<b[1] ? -1 : (a[1]>b[1] ? 1 : 0))});
|
||||
print(ary);
|
||||
|
||||
/* a stable sort will output ["US", "Birmingham"] before ["UK", "Birmingham"] */
|
||||
5
Task/Sort-stability/Jq/sort-stability.jq
Normal file
5
Task/Sort-stability/Jq/sort-stability.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[["UK", "London"],
|
||||
["US", "New York"],
|
||||
["US", "Birmingham"],
|
||||
["UK", "Birmingham"]]
|
||||
| sort_by(.[1])
|
||||
10
Task/Sort-stability/Kotlin/sort-stability.kotlin
Normal file
10
Task/Sort-stability/Kotlin/sort-stability.kotlin
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
// version 1.1.51
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val cities = listOf("UK London", "US New York", "US Birmingham", "UK Birmingham")
|
||||
println("Original : $cities")
|
||||
// sort by country
|
||||
println("By country : ${cities.sortedBy { it.take(2) } }")
|
||||
// sort by city
|
||||
println("By city : ${cities.sortedBy { it.drop(3) } }")
|
||||
}
|
||||
8
Task/Sort-stability/Lasso/sort-stability-1.lasso
Normal file
8
Task/Sort-stability/Lasso/sort-stability-1.lasso
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//Single param array:
|
||||
array->sort
|
||||
|
||||
//An array of pairs, order by the right hand element of the pair:
|
||||
with i in array order by #i->second do => { … }
|
||||
|
||||
//The array can also be ordered by multiple values:
|
||||
with i in array order by #i->second, #i->first do => { … }
|
||||
2
Task/Sort-stability/Lasso/sort-stability-2.lasso
Normal file
2
Task/Sort-stability/Lasso/sort-stability-2.lasso
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
local(a = array('UK'='London','US'='New York','US'='Birmingham','UK'='Birmingham'))
|
||||
with i in #a order by #i->second do => {^ #i->first+' - '+#i->second+'\r' ^}
|
||||
2
Task/Sort-stability/Lasso/sort-stability-3.lasso
Normal file
2
Task/Sort-stability/Lasso/sort-stability-3.lasso
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
local(a = array('UK'='London','US'='New York','US'='Birmingham','UK'='Birmingham'))
|
||||
with i in #a order by #i->second, #i->first do => {^ #i->first+' - '+#i->second+'\r' ^}
|
||||
28
Task/Sort-stability/Liberty-BASIC/sort-stability.basic
Normal file
28
Task/Sort-stability/Liberty-BASIC/sort-stability.basic
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
randomize 0.5
|
||||
N=15
|
||||
dim a(N,2)
|
||||
|
||||
for i = 0 to N-1
|
||||
a(i,1)= int(i/5)
|
||||
a(i,2)= int(rnd(1)*5)
|
||||
next
|
||||
|
||||
print "Unsorted by column #2"
|
||||
print "by construction sorted by column #1"
|
||||
for i = 0 to N-1
|
||||
print a(i,1), a(i,2)
|
||||
next
|
||||
|
||||
sort a(), 0, N-1, 2
|
||||
print
|
||||
|
||||
print "After sorting by column #2"
|
||||
print "Notice wrong order by column #1"
|
||||
for i = 0 to N-1
|
||||
print a(i,1), a(i,2),
|
||||
if i=0 then
|
||||
print
|
||||
else
|
||||
if a(i,2) = a(i-1,2) AND a(i,1) < a(i-1,1) then print "bad order" else print
|
||||
end if
|
||||
next
|
||||
26
Task/Sort-stability/M2000-Interpreter/sort-stability-1.m2000
Normal file
26
Task/Sort-stability/M2000-Interpreter/sort-stability-1.m2000
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Module Stable {
|
||||
Inventory queue alfa
|
||||
Stack New {
|
||||
Data "UK", "London","US", "New York","US", "Birmingham", "UK","Birmingham"
|
||||
While not empty {
|
||||
Append alfa, Letter$:=letter$
|
||||
}
|
||||
}
|
||||
sort alfa
|
||||
k=Each(alfa)
|
||||
Document A$
|
||||
NL$={
|
||||
}
|
||||
While k {
|
||||
A$= Eval$(k, k^)+" "+eval$(k)+NL$
|
||||
}
|
||||
Clipboard A$ ' write to clipboard
|
||||
Report A$
|
||||
}
|
||||
Call Stable
|
||||
|
||||
Output:
|
||||
UK London
|
||||
UK Birmingham
|
||||
US New York
|
||||
US Birmingham
|
||||
26
Task/Sort-stability/M2000-Interpreter/sort-stability-2.m2000
Normal file
26
Task/Sort-stability/M2000-Interpreter/sort-stability-2.m2000
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Module Stable1 {
|
||||
Inventory queue alfa
|
||||
Stack New {
|
||||
Data "UK London","US New York","US Birmingham", "UK Birmingham"
|
||||
While not empty {
|
||||
Append alfa, Letter$
|
||||
}
|
||||
}
|
||||
sort alfa
|
||||
k=Each(alfa)
|
||||
Document A$
|
||||
NL$={
|
||||
}
|
||||
While k {
|
||||
A$= Eval$(k, k^)+NL$
|
||||
}
|
||||
Clipboard A$ ' write to clipboard
|
||||
Report A$
|
||||
}
|
||||
Call Stable1
|
||||
|
||||
Output:
|
||||
UK Birmingham
|
||||
UK London
|
||||
US Birmingham
|
||||
US New York
|
||||
26
Task/Sort-stability/M2000-Interpreter/sort-stability-3.m2000
Normal file
26
Task/Sort-stability/M2000-Interpreter/sort-stability-3.m2000
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Module Stable2 {
|
||||
Inventory alfa
|
||||
Stack New {
|
||||
Data "UK London","US New York","US Birmingham", "UK Birmingham"
|
||||
While not empty {
|
||||
Append alfa, Letter$
|
||||
}
|
||||
}
|
||||
sort alfa
|
||||
k=Each(alfa)
|
||||
Document A$
|
||||
NL$={
|
||||
}
|
||||
While k {
|
||||
A$= Eval$(k, k^)+NL$
|
||||
}
|
||||
Clipboard A$ ' write to clipboard
|
||||
Report A$
|
||||
}
|
||||
Call Stable2
|
||||
|
||||
Output:
|
||||
UK Birmingham
|
||||
UK London
|
||||
US Birmingham
|
||||
US New York
|
||||
3
Task/Sort-stability/Mathematica/sort-stability-1.math
Normal file
3
Task/Sort-stability/Mathematica/sort-stability-1.math
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
mylist = {{1, 2, 3}, {4, 5, 6}, {5, 4, 3}, {9, 5, 1}};
|
||||
Sort[mylist, (#1[[2]] < #2[[2]]) &]
|
||||
#[[Ordering[#[[All, 2]]]]] &[mylist]
|
||||
2
Task/Sort-stability/Mathematica/sort-stability-2.math
Normal file
2
Task/Sort-stability/Mathematica/sort-stability-2.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{{1, 2, 3}, {5, 4, 3}, {9, 5, 1}, {4, 5, 6}}
|
||||
{{1, 2, 3}, {5, 4, 3}, {4, 5, 6}, {9, 5, 1}}
|
||||
55
Task/Sort-stability/NetRexx/sort-stability.netrexx
Normal file
55
Task/Sort-stability/NetRexx/sort-stability.netrexx
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
class RCSortStability
|
||||
|
||||
method main(args = String[]) public constant
|
||||
|
||||
cityList = [String "UK London", "US New York", "US Birmingham", "UK Birmingham"]
|
||||
|
||||
cn = String[cityList.length]
|
||||
|
||||
say
|
||||
say "Before sort:"
|
||||
System.arraycopy(cityList, 0, cn, 0, cityList.length)
|
||||
loop city = 0 to cn.length - 1
|
||||
say cn[city]
|
||||
end city
|
||||
|
||||
cCompNm = Comparator CityComparator()
|
||||
Arrays.sort(cn, cCompNm)
|
||||
|
||||
say
|
||||
say "After sort on city:"
|
||||
loop city = 0 to cn.length - 1
|
||||
say cn[city]
|
||||
end city
|
||||
|
||||
say
|
||||
say "Before sort:"
|
||||
System.arraycopy(cityList, 0, cn, 0, cityList.length)
|
||||
loop city = 0 to cn.length - 1
|
||||
say cn[city]
|
||||
end city
|
||||
|
||||
cCompCtry = Comparator CountryComparator()
|
||||
Arrays.sort(cn, cCompCtry)
|
||||
|
||||
say
|
||||
say "After sort on country:"
|
||||
loop city = 0 to cn.length - 1
|
||||
say cn[city]
|
||||
end city
|
||||
say
|
||||
|
||||
return
|
||||
|
||||
class RCSortStability.CityComparator implements Comparator
|
||||
|
||||
method compare(lft = Object, rgt = Object) public binary returns int
|
||||
return (String lft).substring(4).compareTo((String rgt).substring(4))
|
||||
|
||||
class RCSortStability.CountryComparator implements Comparator
|
||||
|
||||
method compare(lft = Object, rgt = Object) public binary returns int
|
||||
return (String lft).substring(0, 2).compareTo((String rgt).substring(0, 2))
|
||||
20
Task/Sort-stability/Nim/sort-stability.nim
Normal file
20
Task/Sort-stability/Nim/sort-stability.nim
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import algorithm
|
||||
|
||||
const Records = [(country: "UK", city: "London"),
|
||||
(country: "US", city: "New York"),
|
||||
(country: "US", city: "Birmingham"),
|
||||
(country: "UK", city: "Birmingham")]
|
||||
|
||||
echo "Original order:"
|
||||
for record in Records:
|
||||
echo record.country, " ", record.city
|
||||
echo()
|
||||
|
||||
echo "Sorted by city name:"
|
||||
for record in Records.sortedByIt(it.city):
|
||||
echo record.country, " ", record.city
|
||||
echo()
|
||||
|
||||
echo "Sorted by country name:"
|
||||
for record in Records.sortedByIt(it.country):
|
||||
echo record.country, " ", record.city
|
||||
43
Task/Sort-stability/OoRexx/sort-stability.rexx
Normal file
43
Task/Sort-stability/OoRexx/sort-stability.rexx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* Rexx */
|
||||
Do
|
||||
cities = .array~of('UK London', 'US New York', 'US Birmingham', 'UK Birmingham',)
|
||||
|
||||
Say; Say 'Original table'
|
||||
Call display cities
|
||||
|
||||
Say; Say 'Unstable sort on city'
|
||||
sorted = cities~copy
|
||||
sorted~sortWith(.ColumnComparator~new(4, 20))
|
||||
Call display sorted
|
||||
|
||||
Say; Say 'Stable sort on city'
|
||||
sorted = cities~copy
|
||||
sorted~stableSortWith(.ColumnComparator~new(4, 20))
|
||||
Call display sorted
|
||||
|
||||
Say; Say 'Unstable sort on country'
|
||||
sorted = cities~copy
|
||||
sorted~sortWith(.ColumnComparator~new(1, 2))
|
||||
Call display sorted
|
||||
|
||||
Say; Say 'Stable sort on country'
|
||||
sorted = cities~copy
|
||||
sorted~stableSortWith(.ColumnComparator~new(1, 2))
|
||||
Call display sorted
|
||||
|
||||
Return
|
||||
End
|
||||
Exit
|
||||
|
||||
display: Procedure
|
||||
Do
|
||||
Use arg CT
|
||||
|
||||
Say '-'~copies(80)
|
||||
Loop c_ over CT
|
||||
Say c_
|
||||
End c_
|
||||
|
||||
Return
|
||||
End
|
||||
Exit
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
DEFINE TEMP-TABLE tt
|
||||
FIELD country AS CHAR FORMAT 'x(2)'
|
||||
FIELD city AS CHAR FORMAT 'x(16)'
|
||||
.
|
||||
|
||||
DEFINE VARIABLE cc AS CHARACTER EXTENT 2.
|
||||
|
||||
CREATE tt. ASSIGN tt.country = 'UK' tt.city = 'London'.
|
||||
CREATE tt. ASSIGN tt.country = 'US' tt.city = 'New York'.
|
||||
CREATE tt. ASSIGN tt.country = 'US' tt.city = 'Birmingham'.
|
||||
CREATE tt. ASSIGN tt.country = 'UK' tt.city = 'Birmingham'.
|
||||
|
||||
cc[1] = 'by country~n~n'.
|
||||
FOR EACH tt BY tt.country BY ROWID( tt ):
|
||||
cc[1] = cc[1] + tt.country + '~t' + tt.city + '~n'.
|
||||
END.
|
||||
|
||||
cc[2] = 'by city~n~n'.
|
||||
FOR EACH tt BY tt.city BY ROWID( tt ):
|
||||
cc[2] = cc[2] + tt.country + '~t' + tt.city + '~n'.
|
||||
END.
|
||||
|
||||
MESSAGE
|
||||
cc[1] SKIP(1) cc[2]
|
||||
VIEW-AS ALERT-BOX.
|
||||
11
Task/Sort-stability/Oz/sort-stability.oz
Normal file
11
Task/Sort-stability/Oz/sort-stability.oz
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
declare
|
||||
Cities = ['UK'#'London'
|
||||
'US'#'New York'
|
||||
'US'#'Birmingham'
|
||||
'UK'#'Birmingham']
|
||||
in
|
||||
%% sort by city; stable because '=<' is reflexiv
|
||||
{Show {Sort Cities fun {$ A B} A.2 =< B.2 end}}
|
||||
|
||||
%% sort by country; NOT stable because '<' is not reflexiv
|
||||
{Show {Sort Cities fun {$ A B} A.1 < B.1 end}}
|
||||
1
Task/Sort-stability/Perl/sort-stability.pl
Normal file
1
Task/Sort-stability/Perl/sort-stability.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
use sort 'stable';
|
||||
26
Task/Sort-stability/Phix/sort-stability.phix
Normal file
26
Task/Sort-stability/Phix/sort-stability.phix
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">test</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"UK"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"London"</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #008000;">"US"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"New York"</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #008000;">"US"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Birmingham"</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #008000;">"UK"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Birmingham"</span><span style="color: #0000FF;">}}</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">---------------------
|
||||
-- probably stable --
|
||||
---------------------</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">cmp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cmp</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)),{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-----------------------
|
||||
-- guaranteed stable --
|
||||
-----------------------</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">tag_cmp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (see note)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">c</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tag_cmp</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)))</span>
|
||||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">),{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
22
Task/Sort-stability/R/sort-stability.r
Normal file
22
Task/Sort-stability/R/sort-stability.r
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# First, define a bernoulli sample, of length 26.
|
||||
x <- sample(c(0, 1), 26, replace=T)
|
||||
|
||||
x
|
||||
# [1] 1 1 1 1 0 1 1 0 1 0 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0
|
||||
|
||||
# Give names to the entries. "letters" is a builtin value
|
||||
names(x) <- letters
|
||||
|
||||
x
|
||||
# a b c d e f g h i j k l m n o p q r s t u v w x y z
|
||||
# 1 1 1 1 0 1 1 0 1 0 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0
|
||||
|
||||
# The unstable one, see how "a" appears after "l" now
|
||||
sort(x, method="quick")
|
||||
# z h s u e q x n j r t v w y p o m l a i g f d c b k
|
||||
# 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
|
||||
|
||||
# The stable sort, letters are ordered in each section
|
||||
sort(x, method="shell")
|
||||
# e h j n q s u x z a b c d f g i k l m o p r t v w y
|
||||
# 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
|
||||
18
Task/Sort-stability/REBOL/sort-stability.rebol
Normal file
18
Task/Sort-stability/REBOL/sort-stability.rebol
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
; REBOL's sort function is not stable by default. You need to use a custom comparator to make it so.
|
||||
|
||||
blk: [
|
||||
[UK London]
|
||||
[US New-York]
|
||||
[US Birmingham]
|
||||
[UK Birmingham]
|
||||
]
|
||||
sort/compare blk func [a b] [either a/2 < b/2 [-1] [either a/2 > b/2 [1] [0]]]
|
||||
|
||||
; Note that you can also do a stable sort without nested blocks.
|
||||
blk: [
|
||||
UK London
|
||||
US New-York
|
||||
US Birmingham
|
||||
UK Birmingham
|
||||
]
|
||||
sort/skip/compare blk 2 func [a b] [either a < b [-1] [either a > b [1] [0]]]
|
||||
23
Task/Sort-stability/REXX/sort-stability.rexx
Normal file
23
Task/Sort-stability/REXX/sort-stability.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/*REXX program sorts a (stemmed) array using a (stable) bubble─sort algorithm. */
|
||||
call gen@ /*generate the array elements (strings)*/
|
||||
call show 'before sort' /*show the before array elements. */
|
||||
say copies('▒', 50) /*show a separator line between shows. */
|
||||
call bubbleSort # /*invoke the bubble sort. */
|
||||
call show ' after sort' /*show the after array elements. */
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
bubbleSort: procedure expose @.; parse arg n; m= n-1 /*N: number of array elements. */
|
||||
do m=m for m by -1 until ok; ok= 1 /*keep sorting array until done.*/
|
||||
do j=1 for m; k= j+1; if @.j<=@.k then iterate /*Not out─of─order?*/
|
||||
_= @.j; @.j= @.k; @.k= _ ok= 0 /*swap 2 elements; flag as ¬done*/
|
||||
end /*j*/
|
||||
end /*m*/; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gen@: @.=; @.1 = 'UK London'
|
||||
@.2 = 'US New York'
|
||||
@.3 = 'US Birmingham'
|
||||
@.4 = 'UK Birmingham'
|
||||
do #=1 while @.#\=='' /*determine how many entries in list. */
|
||||
end /*#*/; #= # - 1; return /*adjust for the DO loop index; return*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)":" @.j; end; return
|
||||
17
Task/Sort-stability/Racket/sort-stability.rkt
Normal file
17
Task/Sort-stability/Racket/sort-stability.rkt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#lang racket
|
||||
|
||||
(sort '(("UK" "London")
|
||||
("US" "New York")
|
||||
("US" "Birmingham")
|
||||
("UK" "Birmingham"))
|
||||
string<? #:key first)
|
||||
;; -> (("UK" "London") ("UK" "Birmingham")
|
||||
;; ("US" "New York") ("US" "Birmingham"))
|
||||
|
||||
(sort '(("UK" "London")
|
||||
("US" "New York")
|
||||
("US" "Birmingham")
|
||||
("UK" "Birmingham"))
|
||||
string<? #:key second)
|
||||
;; -> '(("US" "Birmingham") ("UK" "Birmingham")
|
||||
;; ("UK" "London") ("US" "New York"))
|
||||
9
Task/Sort-stability/Raku/sort-stability.raku
Normal file
9
Task/Sort-stability/Raku/sort-stability.raku
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use v6;
|
||||
my @cities =
|
||||
['UK', 'London'],
|
||||
['US', 'New York'],
|
||||
['US', 'Birmingham'],
|
||||
['UK', 'Birmingham'],
|
||||
;
|
||||
|
||||
.say for @cities.sort: { .[1] };
|
||||
5
Task/Sort-stability/Ring/sort-stability.ring
Normal file
5
Task/Sort-stability/Ring/sort-stability.ring
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
aList = [["UK", "London"],
|
||||
["US", "New York"],
|
||||
["US", "Birmingham"],
|
||||
["UK", "Birmingham"]]
|
||||
see sort(aList,2)
|
||||
7
Task/Sort-stability/Ruby/sort-stability-1.rb
Normal file
7
Task/Sort-stability/Ruby/sort-stability-1.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
ary = [["UK", "London"],
|
||||
["US", "New York"],
|
||||
["US", "Birmingham"],
|
||||
["UK", "Birmingham"]]
|
||||
p ary.sort {|a,b| a[1] <=> b[1]}
|
||||
# MRI reverses the Birminghams:
|
||||
# => [["UK", "Birmingham"], ["US", "Birmingham"], ["UK", "London"], ["US", "New York"]]
|
||||
20
Task/Sort-stability/Ruby/sort-stability-2.rb
Normal file
20
Task/Sort-stability/Ruby/sort-stability-2.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
class Array
|
||||
def stable_sort
|
||||
n = -1
|
||||
if block_given?
|
||||
collect {|x| n += 1; [x, n]
|
||||
}.sort! {|a, b|
|
||||
c = yield a[0], b[0]
|
||||
if c.nonzero? then c else a[1] <=> b[1] end
|
||||
}.collect! {|x| x[0]}
|
||||
else
|
||||
sort_by {|x| n += 1; [x, n]}
|
||||
end
|
||||
end
|
||||
|
||||
def stable_sort_by
|
||||
block_given? or return enum_for(:stable_sort_by)
|
||||
n = -1
|
||||
sort_by {|x| n += 1; [(yield x), n]}
|
||||
end
|
||||
end
|
||||
8
Task/Sort-stability/Ruby/sort-stability-3.rb
Normal file
8
Task/Sort-stability/Ruby/sort-stability-3.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
ary = [["UK", "London"],
|
||||
["US", "New York"],
|
||||
["US", "Birmingham"],
|
||||
["UK", "Birmingham"]]
|
||||
p ary.stable_sort {|a, b| a[1] <=> b[1]}
|
||||
# => [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]
|
||||
p ary.stable_sort_by {|x| x[1]}
|
||||
# => [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]
|
||||
29
Task/Sort-stability/Rust/sort-stability.rust
Normal file
29
Task/Sort-stability/Rust/sort-stability.rust
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
fn main() {
|
||||
let country_city = [
|
||||
("UK", "London"),
|
||||
("US", "New York"),
|
||||
("US", "Birmingham"),
|
||||
("UK", "Birmingham"),
|
||||
];
|
||||
|
||||
let mut city_sorted = country_city.clone();
|
||||
city_sorted.sort_by_key(|k| k.1);
|
||||
|
||||
let mut country_sorted = country_city.clone();
|
||||
country_sorted.sort_by_key(|k| k.0);
|
||||
|
||||
println!("Original:");
|
||||
for x in &country_city {
|
||||
println!("{} {}", x.0, x.1);
|
||||
}
|
||||
|
||||
println!("\nWhen sorted by city:");
|
||||
for x in &city_sorted {
|
||||
println!("{} {}", x.0, x.1);
|
||||
}
|
||||
|
||||
println!("\nWhen sorted by county:");
|
||||
for x in &country_sorted {
|
||||
println!("{} {}", x.0, x.1);
|
||||
}
|
||||
}
|
||||
19
Task/Sort-stability/Scala/sort-stability-1.scala
Normal file
19
Task/Sort-stability/Scala/sort-stability-1.scala
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
scala> val list = List((1, 'c'), (1, 'b'), (2, 'a'))
|
||||
list: List[(Int, Char)] = List((1,c), (1,b), (2,a))
|
||||
|
||||
scala> val srt1 = list.sortWith(_._2 < _._2)
|
||||
srt1: List[(Int, Char)] = List((2,a), (1,b), (1,c))
|
||||
|
||||
scala> val srt2 = srt1.sortBy(_._1) // Ordering[Int] is implicitly defined
|
||||
srt2: List[(Int, Char)] = List((1,b), (1,c), (2,a))
|
||||
|
||||
scala> val cities = """
|
||||
| |UK London
|
||||
| |US New York
|
||||
| |US Birmingham
|
||||
| |UK Birmingham
|
||||
| |""".stripMargin.lines.filterNot(_ isEmpty).toSeq
|
||||
cities: Seq[String] = ArrayBuffer(UK London, US New York, US Birmingham, UK Birmingham)
|
||||
|
||||
scala> cities.sortBy(_ substring 4)
|
||||
res47: Seq[String] = ArrayBuffer(US Birmingham, UK Birmingham, UK London, US New York)
|
||||
7
Task/Sort-stability/Scala/sort-stability-2.scala
Normal file
7
Task/Sort-stability/Scala/sort-stability-2.scala
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
scala> val cityArray = cities.toArray
|
||||
cityArray: Array[String] = Array(UK London, US New York, US Birmingham, UK Birmingham)
|
||||
|
||||
scala> scala.util.Sorting.stableSort(cityArray, (_: String).substring(4) < (_: String).substring(4))
|
||||
|
||||
scala> cityArray
|
||||
res56: Array[String] = Array(US Birmingham, UK Birmingham, UK London, US New York)
|
||||
10
Task/Sort-stability/Sidef/sort-stability.sidef
Normal file
10
Task/Sort-stability/Sidef/sort-stability.sidef
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
var table = [
|
||||
<UK London>,
|
||||
<US New\ York>,
|
||||
<US Birmingham>,
|
||||
<UK Birmingham>,
|
||||
];
|
||||
|
||||
table.sort {|a,b| a[0] <=> b[0]}.each { |col|
|
||||
say "#{col[0]} #{col[1]}"
|
||||
}
|
||||
22
Task/Sort-stability/Wren/sort-stability.wren
Normal file
22
Task/Sort-stability/Wren/sort-stability.wren
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import "/sort" for Cmp, Sort
|
||||
|
||||
var data = [ ["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"] ]
|
||||
|
||||
// for sorting by country
|
||||
var cmp = Fn.new { |p1, p2| Cmp.string.call(p1[0], p2[0]) }
|
||||
|
||||
// for sorting by city
|
||||
var cmp2 = Fn.new { |p1, p2| Cmp.string.call(p1[1], p2[1]) }
|
||||
|
||||
System.print("Initial data:")
|
||||
System.print(" " + data.join("\n "))
|
||||
|
||||
System.print("\nSorted by country:")
|
||||
var data2 = data.toList
|
||||
Sort.insertion(data2, cmp)
|
||||
System.print(" " + data2.join("\n "))
|
||||
|
||||
System.print("\nSorted by city:")
|
||||
var data3 = data.toList
|
||||
Sort.insertion(data3, cmp2)
|
||||
System.print(" " + data3.join("\n "))
|
||||
2
Task/Sort-stability/Zkl/sort-stability-1.zkl
Normal file
2
Task/Sort-stability/Zkl/sort-stability-1.zkl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fcn sortByColumn(list,col)
|
||||
{ list.sort('wrap(city1,city2){ city1[col]<city2[col] }) }
|
||||
5
Task/Sort-stability/Zkl/sort-stability-2.zkl
Normal file
5
Task/Sort-stability/Zkl/sort-stability-2.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
cities:=List(
|
||||
T("UK", "London"), T("US", "New York"),
|
||||
T("US", "Birmingham"),T("UK", "Birmingham"), );
|
||||
sortByColumn(cities,0).concat("\n").println("\n------");
|
||||
sortByColumn(cities,1).concat("\n").println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue