Jump to content

Sorting algorithms/Comb sort: Difference between revisions

(jq)
Line 385:
{{out}}
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
=={{header|Eiffel}}==
<lang Eiffel>
class
COMB_SORT[G -> COMPARABLE]
feature
combsort (ar: ARRAY[G]): ARRAY[G]
require
array_not_empty: ar.count >0
local
gap, i: INTEGER
swap: G
swapped: BOOLEAN
shrink: REAL_64
do
gap:= ar.count
from
 
until
gap= 1 and swapped = false
loop
from
i:= 1
swapped:= false
until
i+gap > ar.count
loop
if ar[i]> ar[i+gap] then
swap:= ar[i]
ar[i]:= ar[i+gap]
ar[i+gap]:= swap
swapped:= TRUE
end
i:= i+1
end
shrink:= gap/1.3
gap:= shrink.floor
if gap <1 then
gap:= 1
end
end
RESULT:= ar
ensure
RESULT_is_set: Result /= VOID
 
end
end
</lang>
Test:
<lang Eiffel>
class
APPLICATION
 
inherit
ARGUMENTS
 
create
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
across combsort.combsort(test) as ar loop io.put_string (ar.item.out + "%T") end
end
combsort: COMB_SORT[INTEGER]
test: ARRAY[INTEGER]
end
</lang>
{{out}}
<pre>
unsorted:
1 5 99 2 95 7 -7
sorted:
-7 1 2 5 7 95 99
</pre>
 
=={{header|Forth}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.