Jump to content

Sorting algorithms/Comb sort: Difference between revisions

no edit summary
(implementation for 0-based array)
No edit summary
Line 1,422:
 
print(unpack(combsort{3,5,1,2,7,4,8,3,6,4,1}))</lang>
 
=={{header|Maple}}==
<lang Maple>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;</lang>
{{Out|Output}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
 
=={{header|Mathematica}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.