all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
14
Task/Sort-stability/0DESCRIPTION
Normal file
14
Task/Sort-stability/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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.
|
||||
|
||||
For 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).
|
||||
|
||||
#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.
|
||||
|
||||
(This [[wp:Stable_sort#Comparison_of_algorithms|Wikipedia table]] shows the stability of some common sort routines).
|
||||
2
Task/Sort-stability/1META.yaml
Normal file
2
Task/Sort-stability/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Sorting
|
||||
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
|
||||
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"] */
|
||||
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))
|
||||
|
|
@ -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}}
|
||||
9
Task/Sort-stability/Perl-6/sort-stability.pl6
Normal file
9
Task/Sort-stability/Perl-6/sort-stability.pl6
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use v6;
|
||||
my @cities =
|
||||
['UK', 'London'],
|
||||
['US', 'New York'],
|
||||
['US', 'Birmingham'],
|
||||
['UK', 'Birmingham'],
|
||||
;
|
||||
|
||||
.say for @cities.sort: { .[1] };
|
||||
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';
|
||||
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]]]
|
||||
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"]]
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue