Published by Dimitris Kougioumtzis · Updated
Shrink the comparison gap before finishing with neighboring comparisons.
Compare values across a shrinking gap, then repeat gap-one passes until no exchange is necessary.
When to use Comb sort
Studying long-range movement in exchange sorting.
Choosing an alternative
Shell sort sorts gapped subsequences rather than individual pairs.
Worked example: input and state changes
Example input
38, 12, 65, 8, 43, 27, 91, 19
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 38, 12, 65, 8, 43, 27, 91, 19 ], "active": [] }Step 10: Compare across gap 3.
State at this checkpoint
{ "values": [ 8, 12, 65, 38, 43, 27, 91, 19 ], "active": [ 2, 5 ] }Step 20: Compare across gap 2.
State at this checkpoint
{ "values": [ 8, 12, 19, 38, 27, 65, 91, 43 ], "active": [ 4, 6 ] }Step 29: Compare across gap 1.
State at this checkpoint
{ "values": [ 8, 12, 19, 27, 38, 43, 91, 65 ], "active": [ 5, 6 ] }Step 39: Complete: values are in ascending order.
State at this checkpoint
{ "values": [ 8, 12, 19, 27, 38, 43, 65, 91 ], "active": [] }
Expected final state
Complete: values are in ascending order.
Complexity and input bounds
Family: Sorting. Time complexity: O(n²) worst.
Space complexity: O(1) auxiliary space.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
2ā24 comma-separated integers between 0 and 99.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Reaching gap one alone does not prove the array is sorted.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 | // Sorts a in place. n is the array length. Return value 0 indicates completion. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n) { let gap = n; let changed = 1; while (gap > 1 || changed == 1) { gap = Math.floor(gap * 10 / 13); if (gap < 1) { gap = 1; } changed = 0; for (let i = 0; i + gap < n; i++) { if (a[i] > a[i + gap]) { let t = a[i]; a[i] = a[i + gap]; a[i + gap] = t; changed = 1; } } } return 0; } |
Source
Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.
- An efficient variation of bubble sort
Wlodzimierz Dobosiewicz (1980). Research paper.