Comb Sort
Bubble-like sort that eliminates small-value turtles using a shrinking gap between compared elements.
Visualization
Interactive visualization for Comb Sort
Comb Sort Visualization
Interactive visualization with step-by-step execution
Implementation
1function combSort(arr: number[]): number[] {
2 const a = [...arr];
3 const shrink = 1.3;
4 let gap = a.length;
5 let swapped = true;
6 while (gap > 1 || swapped) {
7 gap = Math.floor(gap / shrink);
8 if (gap < 1) gap = 1;
9 swapped = false;
10 for (let i = 0; i + gap < a.length; i++) {
11 if (a[i] > a[i + gap]) { [a[i], a[i + gap]] = [a[i + gap], a[i]]; swapped = true; }
12 }
13 }
14 return a;
15}Deep Dive
Theoretical Foundation
Uses a gap that shrinks by a constant factor (~1.3). Items gap apart are compared/swapped, which quickly removes small-value elements near the end; final passes with gap=1 finish ordering.
Complexity
Time
O(n log n)
O(n²)
O(n²)
Space
O(1)
Applications
Industry Use
Simple improvement over bubble in toy contexts
Small to medium arrays
Use Cases
Related Algorithms
Quicksort
A highly efficient, in-place sorting algorithm that uses divide-and-conquer strategy. Invented by Tony Hoare in 1959, it remains one of the most widely used sorting algorithms due to its excellent average-case performance and cache efficiency.
Merge Sort
A stable, divide-and-conquer sorting algorithm with guaranteed O(n log n) performance.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the entire array is sorted. Named for the way larger elements 'bubble' to the top (end) of the array.
Insertion Sort
Insertion Sort is a simple, intuitive sorting algorithm that builds the final sorted array one element at a time. It works similarly to how people sort playing cards in their hands - picking each card and inserting it into its correct position among the already sorted cards. Despite its O(n²) time complexity, Insertion Sort is efficient for small datasets and nearly sorted arrays, making it practical for real-world applications.