Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,38 +1,63 @@
class
GNOME_SORT
GNOME_SORT [G -> COMPARABLE]
feature
sort(ar: ARRAY[INTEGER]): ARRAY[INTEGER]
-- sort array ar with gnome sort
require
array_not_void: ar/= VOID
local
i,j, ith: INTEGER
do
from
i:= 2
j:= 3
until
i>ar.count
loop
if ar[i-1] <= ar[i] then
i:= j
j:= j+1
else
ith := ar[i-1]
ar[i-1] := ar[i]
ar[i] := ith
i:= i-1
if i=1 then
i:=j
j:= j+1
sort (ar: ARRAY [G]): ARRAY [G]
-- Sorted array in ascending order.
require
array_not_void: ar /= Void
local
i, j: INTEGER
ith: G
do
create Result.make_empty
Result.deep_copy (ar)
from
i := 2
j := 3
until
i > Result.count
loop
if Result [i - 1] <= Result [i] then
i := j
j := j + 1
else
ith := Result [i - 1]
Result [i - 1] := Result [i]
Result [i] := ith
i := i - 1
if i = 1 then
i := j
j := j + 1
end
end
end
end
Result := ar
ensure
same_length: ar.count = Result.count
same_items: Result.same_items (ar)
end
Same_length: ar.count = Result.count
Result_is_sorted: is_sorted (Result)
end
feature {NONE}
is_sorted (ar: ARRAY [G]): BOOLEAN
--- Is 'ar' sorted in ascending order?
require
ar_not_empty: ar.is_empty = False
local
i: INTEGER
do
Result := True
from
i := ar.lower
until
i = ar.upper
loop
if ar [i] > ar [i + 1] then
Result := False
end
i := i + 1
end
end
end

View file

@ -1,21 +1,33 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
make
feature
make
do
test:= <<7, 99, -7, 1, 0, 25, -10>>
create gnome
test:= gnome.sort (test)
across test as ar loop io.put_string( ar.item.out + "%T") end
end
test: ARRAY[INTEGER]
gnome: GNOME_SORT[INTEGER]
make
do
test := <<7, 99, -7, 1, 0, 25, -10>>
io.put_string ("unsorted:%N")
across
test as ar
loop
io.put_string (ar.item.out + "%T")
end
io.new_line
io.put_string ("sorted:%N")
create gnome
test := gnome.sort (test)
across
test as ar
loop
io.put_string (ar.item.out + "%T")
end
end
test: ARRAY [INTEGER]
gnome: GNOME_SORT [INTEGER]
end

View file

@ -0,0 +1,10 @@
defmodule Sort do
def gnome_sort(list) when length(list) <= 1, do: list
def gnome_sort([h|t]), do: gnome_sort([h], t)
defp gnome_sort(list, []), do: list
defp gnome_sort([prev|p], [next|n]) when next > prev, do: gnome_sort(p, [next|[prev|n]])
defp gnome_sort(p, [next|n]), do: gnome_sort([next|p], n)
end
IO.inspect Sort.gnome_sort([8,3,9,1,3,2,6])

View file

@ -0,0 +1,22 @@
function gnomesort(A::AbstractVector)
i = 1
j = 2
while i < length(A)
if A[i] <= A[i + 1]
i = j
j += 1
else
A[i], A[i + 1] = A[i + 1], A[i]
i -= 1
if i == 0
i = j
j += 1
end
end
end
A
end
A = randcycle(20)
println("unsorted: ", A)
println("sorted: ", gnomesort(A))

View file

@ -0,0 +1,28 @@
SORT: PROCEDURE OPTIONS (MAIN);
DECLARE A(0:9) FIXED STATIC INITIAL (5, 2, 7, 1, 9, 8, 6, 3, 4, 0);
CALL GNOME_SORT (A);
put skip edit (A) (f(7));
GNOME_SORT: PROCEDURE (A) OPTIONS (REORDER); /* 9 September 2015 */
declare A(*) fixed;
declare t fixed;
declare (i, j) fixed;
i = 1; j = 2;
do while (i <= hbound(A));
if a(i-1) <= a(i) then
do;
i = j; j = j + 1;
end;
else
do;
t = a(i-1); a(i-1) = a(i); a(i) = t;
i = i - 1;
if i = 0 then do; i = j; j = j + 1; end;
end;
end;
END GNOME_SORT;
END SORT;

View file

@ -0,0 +1,20 @@
function gnomesort($a) {
$size, $i, $j = $a.Count, 1, 2
while($i -lt $size) {
if ($a[$i-1] -le $a[$i]) {
$i = $j
$j++
}
else {
$a[$i-1], $a[$i] = $a[$i], $a[$i-1]
$i--
if($i -eq 0) {
$i = $j
$j++
}
}
}
$a
}
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11)
"$(gnomesort $array)"

View file

@ -1,31 +1,25 @@
/*REXX program sorts a stemmed array using the gnome-sort algorithm.*/
call gen@ /*generate the @. array elements.*/
call show@ 'before sort' /*show "before" array elements.*/
call gnomeSort # /*invoke the infamous gnome sort.*/
call show@ ' after sort' /*show "after" array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────GNOMESORT subroutine────────────────*/
gnomeSort: procedure expose @.; parse arg n; k=2 /*n=num items.*/
do j=3 while k<=n; km=k-1 /*KM=prev item*/
if @.km<<=@.k then do; k=j; iterate; end /*OK so far···*/
_=@.km; @.km=@.k; @.k=_ /*swap 2 entries in the @. array.*/
k=k-1; if k==1 then k=j; else j=j-1 /*test index 1*/
end /*j*/ /* [↑] perform gnome sort on @.*/
/*REXX program sorts a stemmed array using the gnome sort algorithm. */
call gen; w=length(#) /*generate @ array; W is width of #.*/
call show 'before sort' /*display the "before" array elements.*/
say copies('',60) /*show a separator line between sorts. */
call gnomeSort # /*invoke the well─known gnome sort. */
call show ' after sort' /*display the "after" array elements.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────GNOMESORT subroutine──────────────────────*/
gnomeSort: procedure expose @.; parse arg n; k=2 /*N: is number items.*/
do j=3 while k<=n; p=k-1 /*P: is previous item*/
if @.p<<=@.k then do; k=j; iterate; end /*array is OK so far.*/
_=@.p; @.p=@.k; @.k=_ /*swap two @ entries.*/
k=k-1; if k==1 then k=j; else j=j-1 /*test for 1st index.*/
end /*j*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: !=... 'deadbeef'x ...; @.=! /*default none-value; allows null*/
@.1 = '---the seven virtues---' /* [↓] indent the seven virtues.*/
@.2 = '=======================' ; @.6 = 'Fortitude'
@.3 = 'Faith' ; @.7 = 'Justice'
@.4 = 'Hope' ; @.8 = 'Prudence'
@.5 = 'Charity [Love]' ; @.9 = 'Temperance'
do #=1 while @.#\==!; end /*find the # of items in @ array.*/
#=#-1 /*adjust the numer of items by 1.*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do j=1 for # /* [↓] display all items for @. */
say ' element' right(j,length(#)) arg(1)":" @.j
end /*j*/ /* [↑] right justify the J num.*/
say copies('',60) /*show a separator line that fits*/
/*──────────────────────────────────GEN subroutine────────────────────────────*/
gen: @.=; @.1 = '---the seven virtues---' ; @.5 = 'Charity [Love]'
@.2 = '=======================' ; @.6 = 'Fortitude'
@.3 = 'Faith' ; @.7 = 'Justice'
@.4 = 'Hope' ; @.8 = 'Prudence'
@.9 = 'Temperance'
do #=1 while @.#\==''; end; #=#-1 /*determine number of items in @ array.*/
return
/*──────────────────────────────────SHOW subroutine───────────────────────────*/
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return