File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments