Sorting algorithms/Comb sort: Difference between revisions

Added a solution for MATLAB
No edit summary
(Added a solution for MATLAB)
Line 363:
 
print(unpack(combsort{3,5,1,2,7,4,8,3,6,4,1}))</lang>
 
=={{header|MATLAB}}==
<lang MATLAB>function list = combSort(list)
listSize = numel(list);
gap = int32(listSize); %Coerce gap to an int so we can use the idivide function
swaps = true; %Swap flag
while not((gap <= 1) && (swaps == false))
gap = idivide(gap,1.25,'floor'); %Int divide, floor the resulting operation
if gap < 1
gap = 1;
end
i = 1; %i equals 1 because all arrays are 1 based in MATLAB
swaps = false;
%i + gap must be subtracted by 1 because the pseudo-code was writen
%for 0 based arrays
while not((i + gap - 1) >= listSize)
if (list(i) > list(i+gap))
temp = list(i);
list(i) = list(i+gap);
list(i+gap) = temp;
swaps = true;
end
i = i + 1;
end %while
end %while
end %combSort</lang>
 
Sample Output:
<lang MATLAB>>> combSort([4 3 1 5 6 2])
 
ans =
 
1 2 3 4 5 6</lang>
 
=={{header|OCaml}}==
Anonymous user