Patience Sort
Card-game-inspired sorting building piles with binary search and merging pile tops via a min-heap.
Visualization
Interactive visualization for Patience Sort
Patience Sort Visualization
Interactive visualization with step-by-step execution
Implementation
1function patienceSort(arr: number[]): number[] {
2 const piles: number[][] = [];
3 const tops: number[] = [];
4 for (const x of arr) {
5 let l = 0, r = tops.length;
6 while (l < r) { const m = (l + r) >> 1; if (tops[m] >= x) r = m; else l = m + 1; }
7 if (l === tops.length) { piles.push([x]); tops.push(x); }
8 else { piles[l].push(x); tops[l] = x; }
9 }
10 type Node = { value: number; pile: number };
11 const heap: Node[] = [];
12 const up = (i:number)=>{ while(i>0){ const p=(i-1)>>1; if(heap[p].value<=heap[i].value) break; [heap[p],heap[i]]=[heap[i],heap[p]]; i=p; } };
13 const dn = (i:number)=>{ const n=heap.length; for(;;){ let a=2*i+1,b=a+1,m=i; if(a<n&&heap[a].value<heap[m].value) m=a; if(b<n&&heap[b].value<heap[m].value) m=b; if(m===i) break; [heap[i],heap[m]]=[heap[m],heap[i]]; i=m; } };
14 const push=(n:Node)=>{ heap.push(n); up(heap.length-1); };
15 const pop=():Node|undefined=>{ if(!heap.length) return; const r=heap[0]; const last=heap.pop()!; if(heap.length){ heap[0]=last; dn(0);} return r; };
16 for (let p = 0; p < piles.length; p++) { const v = piles[p].pop()!; push({ value: v, pile: p }); }
17 const out: number[] = [];
18 while (heap.length) { const n = pop()!; out.push(n.value); if (piles[n.pile].length) push({ value: piles[n.pile].pop()!, pile: n.pile }); }
19 return out;
20}Deep Dive
Theoretical Foundation
Create piles by placing each element on the leftmost pile whose top is ≥ it (found by binary search on pile tops). Then perform a k-way merge using a min-heap of pile tops to produce the sorted sequence.
Complexity
Time
O(n log n)
O(n log n)
O(n log n)
Space
O(n)
Applications
Industry Use
Educational (LIS connection)
Sequence analysis and run detection
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.