Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,44 +1,70 @@
|
|||
class
|
||||
COMB_SORT[G -> COMPARABLE]
|
||||
COMB_SORT [G -> COMPARABLE]
|
||||
|
||||
feature
|
||||
combsort (ar: ARRAY[G]): ARRAY[G]
|
||||
|
||||
combsort (ar: ARRAY [G]): ARRAY [G]
|
||||
-- Sorted array in ascending order.
|
||||
require
|
||||
array_not_empty: ar.count >0
|
||||
array_not_void: ar /= Void
|
||||
local
|
||||
gap, i: INTEGER
|
||||
swap: G
|
||||
swapped: BOOLEAN
|
||||
shrink: REAL_64
|
||||
do
|
||||
gap:= ar.count
|
||||
create Result.make_empty
|
||||
Result.deep_copy (ar)
|
||||
gap := Result.count
|
||||
from
|
||||
|
||||
until
|
||||
gap= 1 and swapped = false
|
||||
gap = 1 and swapped = False
|
||||
loop
|
||||
from
|
||||
i:= 1
|
||||
swapped:= false
|
||||
i := Result.lower
|
||||
swapped := False
|
||||
until
|
||||
i+gap > ar.count
|
||||
i + gap > Result.count
|
||||
loop
|
||||
if ar[i]> ar[i+gap] then
|
||||
swap:= ar[i]
|
||||
ar[i]:= ar[i+gap]
|
||||
ar[i+gap]:= swap
|
||||
swapped:= TRUE
|
||||
if Result [i] > Result [i + gap] then
|
||||
swap := Result [i]
|
||||
Result [i] := Result [i + gap]
|
||||
Result [i + gap] := swap
|
||||
swapped := True
|
||||
end
|
||||
i:= i+1
|
||||
i := i + 1
|
||||
end
|
||||
shrink:= gap/1.3
|
||||
gap:= shrink.floor
|
||||
if gap <1 then
|
||||
gap:= 1
|
||||
shrink := gap / 1.3
|
||||
gap := shrink.floor
|
||||
if gap < 1 then
|
||||
gap := 1
|
||||
end
|
||||
end
|
||||
RESULT:= ar
|
||||
ensure
|
||||
RESULT_is_set: Result /= VOID
|
||||
|
||||
ensure
|
||||
Result_is_set: Result /= Void
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,24 +1,32 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
inherit
|
||||
ARGUMENTS
|
||||
|
||||
create
|
||||
make
|
||||
make
|
||||
|
||||
feature
|
||||
make
|
||||
|
||||
do
|
||||
test:= <<1,5,99,2,95, 7,-7>>
|
||||
io.put_string ("unsorted"+"%N")
|
||||
across test as ar loop io.put_string(ar.item.out + "%T") end
|
||||
io.put_string ("%N"+"sorted:"+"%N")
|
||||
create combsort
|
||||
test:=combsort.combsort(test)
|
||||
across test as ar loop io.put_string (ar.item.out + "%T") end
|
||||
end
|
||||
combsort: COMB_SORT[INTEGER]
|
||||
test: ARRAY[INTEGER]
|
||||
make
|
||||
do
|
||||
test := <<1, 5, 99, 2, 95, 7, -7>>
|
||||
io.put_string ("unsorted" + "%N")
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + "%T")
|
||||
end
|
||||
io.put_string ("%N" + "sorted:" + "%N")
|
||||
create combsort
|
||||
test := combsort.combsort (test)
|
||||
across
|
||||
test as ar
|
||||
loop
|
||||
io.put_string (ar.item.out + "%T")
|
||||
end
|
||||
end
|
||||
|
||||
combsort: COMB_SORT [INTEGER]
|
||||
|
||||
test: ARRAY [INTEGER]
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
'randomize 0.5
|
||||
itemCount = 20
|
||||
dim item(itemCount)
|
||||
for i = 1 to itemCount
|
||||
item(i) = int(rnd(1) * 100)
|
||||
next i
|
||||
print "Before Sort"
|
||||
for i = 1 to itemCount
|
||||
print item(i)
|
||||
next i
|
||||
print: print
|
||||
't0=time$("ms")
|
||||
|
||||
gap=itemCount
|
||||
while gap>1 or swaps <> 0
|
||||
gap=int(gap/1.25)
|
||||
'if gap = 10 or gap = 9 then gap = 11 'uncomment to get Combsort11
|
||||
if gap <1 then gap = 1
|
||||
i = 1
|
||||
swaps = 0
|
||||
for i = 1 to itemCount-gap
|
||||
if item(i) > item(i + gap) then
|
||||
temp = item(i)
|
||||
item(i) = item(i + gap)
|
||||
item(i + gap) = temp
|
||||
swaps = 1
|
||||
end if
|
||||
next
|
||||
wend
|
||||
|
||||
print "After Sort"
|
||||
't1=time$("ms")
|
||||
'print t1-t0
|
||||
|
||||
for i = 1 to itemCount
|
||||
print item(i)
|
||||
next i
|
||||
end
|
||||
|
|
@ -1,36 +1,34 @@
|
|||
/*REXX program sorts a stemmed array using the comb-sort algorithm. */
|
||||
call gen@; w=length(#) /*generate the array elements. */
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call combSort # /*invoke the comb sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────COMBSORT subroutine─────────────────*/
|
||||
combSort: procedure expose @.; parse arg N /*N: is 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*.8) /* ÷ is slow, * is better.*/
|
||||
/*REXX program sorts a stemmed array using the comb sort algorithm. */
|
||||
call gen; w=length(#) /*generate the @ array elements. */
|
||||
call show 'before sort' /*display the before array elements. */
|
||||
say copies('▒',60) /*display a separator line (a fence). */
|
||||
call combSort # /*invoke the comb sort. */
|
||||
call show ' after sort' /*display the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────COMBSORT subroutine───────────────────────*/
|
||||
combSort: procedure expose @.; parse arg N /*N: is 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*.8) /* ÷ 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*/
|
||||
return
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @. = ; @.12 = 'dodecagon 12'
|
||||
@.1 = '----polygon--- sides' ; @.13 = 'tridecagon 13'
|
||||
@.2 = '============== =======' ; @.14 = 'tetradecagon 14'
|
||||
@.3 = 'triangle 3' ; @.15 = 'pentadecagon 15'
|
||||
@.4 = 'quadrilateral 4' ; @.16 = 'hexadecagon 16'
|
||||
@.5 = 'pentagon 5' ; @.17 = 'heptadecagon 17'
|
||||
@.6 = 'hexagon 6' ; @.18 = 'octadecagon 18'
|
||||
@.7 = 'heptagon 7' ; @.19 = 'enneadecagon 19'
|
||||
@.8 = 'octagon 8' ; @.20 = 'icosagon 20'
|
||||
@.9 = 'nonagon 9' ; @.21 = 'hectogon 100'
|
||||
@.10 = 'decagon 10' ; @.22 = 'chiliagon 1000'
|
||||
@.11 = 'hendecagon 11' ; @.23 = 'myriagon 10000'
|
||||
do #=1 while @.#\==''; end; #=#-1 /*find how many entries in array.*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: say copies('▒',60); do j=1 for # /*display array elements.*/
|
||||
say ' element' right(j,w) arg(1)":" @.j
|
||||
end /*j*/
|
||||
/*──────────────────────────────────GEN subroutine────────────────────────────*/
|
||||
gen: @. = ; @.12 = 'dodecagon 12'
|
||||
@.1 = '----polygon--- sides' ; @.13 = 'tridecagon 13'
|
||||
@.2 = '============== =======' ; @.14 = 'tetradecagon 14'
|
||||
@.3 = 'triangle 3' ; @.15 = 'pentadecagon 15'
|
||||
@.4 = 'quadrilateral 4' ; @.16 = 'hexadecagon 16'
|
||||
@.5 = 'pentagon 5' ; @.17 = 'heptadecagon 17'
|
||||
@.6 = 'hexagon 6' ; @.18 = 'octadecagon 18'
|
||||
@.7 = 'heptagon 7' ; @.19 = 'enneadecagon 19'
|
||||
@.8 = 'octagon 8' ; @.20 = 'icosagon 20'
|
||||
@.9 = 'nonagon 9' ; @.21 = 'hectogon 100'
|
||||
@.10 = 'decagon 10' ; @.22 = 'chiliagon 1000'
|
||||
@.11 = 'hendecagon 11' ; @.23 = 'myriagon 10000'
|
||||
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW subroutine───────────────────────────*/
|
||||
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue