Sorting algorithms/Comb sort: Difference between revisions

+Icon+Unicon
(Add BBC Basic and Cobol versions of Combsort11)
(+Icon+Unicon)
Line 384:
lst := list(23, 76, 99, 58, 97, 57, 35, 89, 51, 38, 95, 92, 24, 46, 31, 24, 14, 12, 57, 78)
lst combSortInPlace println # ==> list(12, 14, 23, 24, 24, 31, 35, 38, 46, 51, 57, 57, 58, 76, 78, 89, 92, 95, 97, 99)</lang>
 
<lang Icon>procedure main() #: demonstrate various ways to sort a list and string
demosort(combsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
 
procedure combsort(X,op) #: return sorted X
local gap,swapped,i
 
op := sortop(op,X) # select how and what we sort
 
swappped := gap := *X # initialize gap size and say swapped
until /swapped & gap = 1 do {
gap := integer(gap / 1.25) # update the gap value for a next comb
gap <:= 1 # minimum gap of 1
swapped := &null
 
i := 0
until (i +:= 1) + gap > *X do # a single "comb" over the input list
if op(X[i+gap],X[i]) then
X[i+1] :=: X[swapped := i] # swap and flag as unsorted
}
return X
end</lang>
 
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]].
 
Sample Output:<pre>Sorting Demo using procedure combsort
on list : [ 3 14 1 5 9 2 6 3 ]
with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms)
with op = "numeric": [ 1 2 3 3 5 6 9 14 ] (0 ms)
with op = "string": [ 1 14 2 3 3 5 6 9 ] (0 ms)
with op = ">>": [ 9 6 5 3 3 2 14 1 ] (0 ms)
with op = ">": [ 14 9 6 5 3 3 2 1 ] (0 ms)
with op = procedure cmp: [ 1 2 3 3 5 6 9 14 ] (0 ms)
with op = "cmp": [ 1 2 3 3 5 6 9 14 ] (0 ms)
on string : "qwerty"
with op = &null: "eqrtwy" (0 ms)</pre>
 
=={{header|J}}==
Anonymous user