September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,37 @@
// version 1.1.0
fun cocktailSort(a: IntArray) {
fun swap(i: Int, j: Int) {
val temp = a[i]
a[i] = a[j]
a[j] = temp
}
do {
var swapped = false
for (i in 0 until a.size - 1)
if (a[i] > a[i + 1]) {
swap(i, i + 1)
swapped = true
}
if (!swapped) break
swapped = false
for (i in a.size - 2 downTo 0)
if (a[i] > a[i + 1]) {
swap(i, i + 1)
swapped = true
}
}
while (swapped)
}
fun main(args: Array<String>) {
val aa = arrayOf(
intArrayOf(100, 2, 56, 200, -52, 3, 99, 33, 177, -199),
intArrayOf(4, 65, 2, -31, 0, 99, 2, 83, 782, 1),
intArrayOf(62, 83, 18, 53, 7, 17, 95, 86, 47, 69, 25, 28)
)
for (a in aa) {
cocktailSort(a)
println(a.joinToString(", "))
}
}

View file

@ -0,0 +1,51 @@
/* Rexx */
placesList = .array~of( -
"UK London", "US New York" , "US Boston", "US Washington" -
, "UK Washington", "US Birmingham" , "UK Birmingham", "UK Boston" -
)
sortedList = cocktailSort(placesList~allItems())
lists = .array~of(placesList, sortedList)
loop ln = 1 to lists~items()
cl = lists[ln]
loop ct = 1 to cl~items()
say cl[ct]
end ct
say
end ln
return
exit
::routine cocktailSort
use arg A
Alength = A~items()
swapped = .false
loop label swaps until \swapped
swapped = .false
loop i_ = 1 to Alength - 1
if A[i_] > A[i_ + 1] then do
swap = A[i_ + 1]
A[i_ + 1] = A[i_]
A[i_] = swap
swapped = .true
end
end i_
if \swapped then do
leave swaps
end
swapped = .false
loop i_ = Alength - 1 to 1 by -1
if A[i_] > A[i_ + 1] then do
swap = A[i_ + 1]
A[i_ + 1] = A[i_]
A[i_] = swap
swapped = .true
end
end i_
end swaps
return A

View file

@ -0,0 +1,25 @@
function varargout=cocktailSort(list_in)
swapped = %T;
while swapped
swapped = %F;
for i = [1:length(list_in)-1]
if list_in(i) > list_in(i+1) then
list_in([i i+1])=list_in([i+1 i]);
swapped = %T;
end
end
if ~swapped
break
end
swapped = %F;
for i = [length(list_in)-1:-1:1]
if list_in(i) > list_in(i+1)
list_in([i i+1]) = list_in([i+1 i])
swapped = %T;
end
end
end
varargout=list(list_in)
endfunction
disp(cocktailSort([6 3 7 8 5 1 2 4 9]));

View file

@ -1,16 +1,16 @@
func cocktailsort(a) {
var swapped = false;
var swapped = false
func cmpsw(i) {
if (a[i] > a[i+1]) {
a[i, i+1] = a[i+1, i];
swapped = true;
a[i, i+1] = a[i+1, i]
swapped = true
}
}
var max = a.end;
var max = a.end
do {
{ |i| cmpsw(i-1) } * max;
swapped.not! && break;
{ |i| cmpsw(max-i) } * max;
} while (swapped);
return a;
{|i| cmpsw(i) } << ^max
swapped.not! && break
{|i| cmpsw(max-i) } << 1..max
} while (swapped)
return a
}

View file

@ -1,5 +1,5 @@
var numbers = [7,6,5,9,8,4,3,1,2,0];
say cocktailsort(numbers).dump;
var numbers = [7,6,5,9,8,4,3,1,2,0]
say cocktailsort(numbers)
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"];
say cocktailsort(strs).dump;
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]
say cocktailsort(strs)

View file

@ -0,0 +1,15 @@
fcn cocktailSort(a){
swapped,begin,end:=False,-1,a.len() - 2;
do{
swapped,begin=False,begin + 1;
foreach i in ([begin .. end]){
if(a[i]>a[i+1]){ a.swap(i,i+1); swapped=True; }
}
if(not swapped) break;
swapped,end=False,end - 1;
foreach i in ([end..begin,-1]){
if(a[i]>a[i+1]){ a.swap(i,i+1); swapped=True; }
}
}while(swapped);
a
}

View file

@ -0,0 +1,5 @@
cocktailSort(List(2,1)).println();
x:=List(5, -1, 101, -4, 0, 1, 8, 6, 2, 3 );
cocktailSort(x).println();
x="the lazy fox jumped over the brown dog".split(" ").copy();
cocktailSort(x).println();