Comb Sort Visualizer
Speed:
SlowFast
Bars:
520
(10)179
[0]
161
[1]
77
[2]
26
[3]
135
[4]
51
[5]
23
[6]
33
[7]
57
[8]
33
[9]
Comparing
Swapping
Sorted
Default
Operation Logs
Click "Start Sort" to begin
Algorithm Info
Time Complexity: O(n²) worst case, O(n log n) average
Space Complexity: O(1) - in-place sorting
Stable: No
How it works: Improves bubble sort by eliminating small values at the end of list by using a gap larger than 1, shrinking by factor of 1.3 each pass.
Implementation
function combSort(arr) {
let n = arr.length;
let gap = n;
let swapped = true;
while (gap !== 1 || swapped) {
gap = Math.max(1, Math.floor(gap / 1.3));
swapped = false;
for (let i = 0; i < n - gap; i++) {
if (arr[i] > arr[i + gap]) {
[arr[i], arr[i + gap]] = [arr[i + gap], arr[i]];
swapped = true;
}
}
}
return arr;
}