Skip to content

Commit e9530c9

Browse files
authored
Merge pull request #72 from vanitaneja22/main
Add comb_sort Algorithm
2 parents 0cef02a + d7175dd commit e9530c9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

algorithms/sorting/comb_sort.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
%This function sorts the input array in ascending order using the Comb Sort algorithm
2+
%For details, refer https://en.wikipedia.org/wiki/Comb_sort
3+
4+
function y = combSort(array)
5+
6+
len = length(array);
7+
k = len;
8+
isSwapped = true;
9+
% value of shrink should be greater than 1
10+
shrink = 1.4;
11+
while ((k > 1) || (isSwapped == true))
12+
k = max(floor(k / shrink),1);
13+
% Bubble sort with given value of k
14+
i = 1;
15+
isSwapped = false;
16+
while ((i + k) <= len)
17+
if (array(i) > array(i + k))
18+
array = swap(array,i,i + k);
19+
isSwapped = true;
20+
end
21+
i = i + 1;
22+
end
23+
end
24+
25+
end
26+
27+
function array = swap(array,i,j)
28+
value = array(i);
29+
array(i) = array(j);
30+
array(j) = value;
31+
% Note: In practice, array should be passed by reference
32+
end

0 commit comments

Comments
 (0)