June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,42 @@
import extensions.
import system'math.
import system'routines.
extension $op
{
combSort
[
var list := self clone.
real gap := list length.
bool swaps := true.
while ((gap > 1)|| (swaps))
[
gap /= 1.247330950103979r.
if (gap<1) [ gap := 1 ].
int i := 0.
swaps := false.
while (i + gap roundedInt < list length)
[
int igap := i + gap roundedInt.
if (list[i] > list[igap])
[
list exchange(i,igap).
swaps := true
].
i += 1
].
].
^ list
]
}
program =
[
var list := (3, 5, 1, 9, 7, 6, 8, 2, 4 ).
console printLine("before:", list).
console printLine("after :", list combSort).
].

View file

@ -0,0 +1,43 @@
Public Sub Main()
Dim siToSort As Short[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 147, 154, 46, 102, 183, 24,
120, 19, 123, 2, 17, 226, 11, 211, 25, 191, 205, 77]
Dim siStart As Short
Dim siGap As Short = siToSort.Max
Dim bSorting, bGap1 As Boolean
Print "To sort: -"
ShowWorking(siToSort)
Print
Repeat
bSorting = False
siStart = 0
If siGap = 1 Then bGap1 = True
Repeat
If siToSort[siStart] > siToSort[siStart + siGap] Then
Swap siToSort[siStart], siToSort[siStart + siGap]
bSorting = True
End If
Inc siStart
Until siStart + siGap > siToSort.Max
If bSorting Then ShowWorking(siToSort)
siGap /= 1.3
If siGap < 1 Then siGap = 1
Until bSorting = False And bGap1 = True
End
'-----------------------------------------
Public Sub ShowWorking(siToSort As Short[])
Dim siCount As Short
For siCount = 0 To siToSort.Max
Print Str(siToSort[siCount]);
If siCount <> siToSort.Max Then Print ",";
Next
Print
End

View file

@ -0,0 +1,32 @@
swap := proc(arr, a, b)
local temp;
temp := arr[a]:
arr[a] := arr[b]:
arr[b] := temp:
end proc:
newGap := proc(gap)
local new;
new := trunc(gap*10/13);
if (new < 1) then return 1; end if;
return new;
end proc;
combsort := proc(arr, len)
local gap, swapped,i, temp;
swapped := true:
gap := len:
while ((not gap = 1) or swapped) do
gap := newGap(gap):
swapped := false:
for i from 1 to len-gap by 1 do
if (arr[i] > arr[i+gap]) then
temp := arr[i]:
arr[i] := arr[i+gap]:
arr[i+gap] := temp:
swapped:= true:
end if:
end do:
end do:
end proc:
arr := Array([17,3,72,0,36,2,3,8,40,0]);
combsort(arr, numelems(arr));
arr;

View file

@ -1,5 +1,8 @@
program CombSortDemo;
// NOTE: The array is 1-based
// If you want to use this code on a 0-based array, see below
type
TIntArray = array[1..40] of integer;

View file

@ -0,0 +1,53 @@
program CombSortDemo;
// NOTE: The array is 0-based
// If you want to use this code on a 1-based array, see above
type
TIntArray = array[0..39] of integer;
var
data: TIntArray;
i: integer;
procedure combSort(var a: TIntArray);
var
i, gap, temp: integer;
swapped: boolean;
begin
gap := length(a);
swapped := true;
while (gap > 1) or swapped do
begin
gap := trunc(gap / 1.3);
if (gap < 1) then
gap := 1;
swapped := false;
for i := 0 to length(a) - gap - 1 do
if a[i] > a[i+gap] then
begin
temp := a[i];
a[i] := a[i+gap];
a[i+gap] := temp;
swapped := true;
end;
end;
end;
begin
Randomize;
writeln('The data before sorting:');
for i := low(data) to high(data) do
begin
data[i] := Random(high(data));
write(data[i]:4);
end;
writeln;
combSort(data);
writeln('The data after sorting:');
for i := low(data) to high(data) do
begin
write(data[i]:4);
end;
writeln;
end.

View file

@ -7,13 +7,14 @@ call show ' after sort' /*display the after array el
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
combSort: procedure expose @.; parse arg N /*N: is the number of @ elements. */
s=N-1 /*S: is the spread between COMBs. */
do until s<=1 & done; done=1 /*assume sort is done (so far). */
s=trunc(s*0.8) /*Note: ÷ is slow, * is better.*/
do j=1 until js>=N; js=j+s
if @.j>@.js then do; _=@.j; @.j=@.js; @.js=_; done=0; end
end /*j*/
end /*until*/
g=N - 1 /*G: is the gap between the sort COMBs*/
do until g<=1 & done; done=1 /*assume sort is done (so far). */
g=g * 0.8 % 1 /*equivalent to: g=trunc( g / 1.25) */
if g==0 then g=1 /*handle case of the gap is too small. */
do j=1 until $ >= N; $=j + g /*$: temp index variable. */
if @.j > @.$ then do; _=@.j; @.j=@.$; @.$=_; done=0; end
end /*j*/
end /*until*/ /* [↑] swap two elements in the array.*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @. = ; @.12 = "dodecagon 12"
@ -31,4 +32,4 @@ gen: @. = ; @.12 = "dodecagon 12
do #=1 while @.#\==''; end; #=#-1 /*find how many elements in @*/
return /* [↑] adjust # because of the DO loop*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return
show: do k=1 for #; say right('element',15) right(k,w) arg(1)":" @.k; end; return

View file

@ -0,0 +1,29 @@
object CombSort extends App {
val ia = Array(28, 44, 46, 24, 19, 2, 17, 11, 25, 4)
val ca = Array('X', 'B', 'E', 'A', 'Z', 'M', 'S', 'L', 'Y', 'C')
def sorted[E](input: Array[E])(implicit ord: Ordering[E]): Array[E] = {
import ord._
var gap = input.length
var swapped = true
while (gap > 1 || swapped) {
if (gap > 1) gap = (gap / 1.3).toInt
swapped = false
for (i <- 0 until input.length - gap)
if (input(i) >= input(i + gap)) {
val t = input(i)
input(i) = input(i + gap)
input(i + gap) = t
swapped = true
}
}
input
}
println(s"Unsorted : ${ia.mkString("[", ", ", "]")}")
println(s"Sorted : ${sorted(ia).mkString("[", ", ", "]")}\n")
println(s"Unsorted : ${ca.mkString("[", ", ", "]")}")
println(s"Sorted : ${sorted(ca).mkString("[", ", ", "]")}")
}