Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions algorithms/sorting/comb_sort.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%This function sorts the input array in ascending order using the Comb Sort algorithm
%For details, refer https://en.wikipedia.org/wiki/Comb_sort

function y = combSort(array)

len = length(array);
k = len;
isSwapped = true;
% value of shrink should be greater than 1
shrink = 1.4;
while ((k > 1) || (isSwapped == true))
k = max(floor(k / shrink),1);
% Bubble sort with given value of k
i = 1;
isSwapped = false;
while ((i + k) <= len)
if (array(i) > array(i + k))
array = swap(array,i,i + k);
isSwapped = true;
end
i = i + 1;
end
end

end

function array = swap(array,i,j)
value = array(i);
array(i) = array(j);
array(j) = value;
% Note: In practice, array should be passed by reference
end