Sorting algorithms/Comb sort: Difference between revisions
Content added Content deleted
(→{{header|MATLAB}}: works fine with Octave, too. Verified with Octave 3.4.3) |
|||
Line 741: | Line 741: | ||
print(unpack(combsort{3,5,1,2,7,4,8,3,6,4,1}))</lang> |
print(unpack(combsort{3,5,1,2,7,4,8,3,6,4,1}))</lang> |
||
=={{header|Mathematica}}== |
|||
<lang Mathematica>combSort[list_] := Module[{ gap = 0, listSize = 0, swaps = True}, |
|||
gap = listSize = Length[list]; |
|||
While[ !((gap <= 1) && (swaps == False)), |
|||
gap = Floor@Divide[gap, 1.25]; |
|||
If[ gap < 1, gap = 1]; i = 1; swaps = False; |
|||
While[ ! ((i + gap - 1) >= listSize), |
|||
If[ list[[i]] > list[[i + gap]], swaps = True; |
|||
list[[i ;; i + gap]] = list[[i + gap ;; i ;; -1]]; |
|||
]; |
|||
i++; |
|||
] |
|||
] |
|||
]</lang> |
|||
<pre>combSort@{2, 1, 3, 7, 6} |
|||
->{1, 2, 3, 6, 7} |
|||
</pre> |
|||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |