|
| 1 | +import type { SortingGenerator } from './types'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Intro Sort (Introsort) Algorithm |
| 5 | + * |
| 6 | + * A hybrid sorting algorithm that combines Quick Sort, Heap Sort, and Insertion Sort. |
| 7 | + * It starts with Quick Sort, switches to Heap Sort when the recursion depth becomes |
| 8 | + * too deep, and uses Insertion Sort for small subarrays. |
| 9 | + * |
| 10 | + * Time Complexity: O(n log n) guaranteed worst case |
| 11 | + * Space Complexity: O(log n) |
| 12 | + * |
| 13 | + * @param arr - Array of numbers to sort |
| 14 | + * @yields ProgressIndicator - Visualization data for each step |
| 15 | + */ |
| 16 | +export const introSort = function* (arr: number[]): SortingGenerator { |
| 17 | + const n = arr.length; |
| 18 | + if (n <= 1) return; |
| 19 | + |
| 20 | + // Calculate maximum recursion depth (2 * log2(n)) |
| 21 | + const maxDepth = Math.floor(2 * Math.log2(n)); |
| 22 | + |
| 23 | + yield* introSortRecursive(arr, 0, n - 1, maxDepth); |
| 24 | +}; |
| 25 | + |
| 26 | +function* introSortRecursive( |
| 27 | + arr: number[], |
| 28 | + low: number, |
| 29 | + high: number, |
| 30 | + depthLimit: number |
| 31 | +): SortingGenerator { |
| 32 | + const size = high - low + 1; |
| 33 | + |
| 34 | + // Use insertion sort for small arrays |
| 35 | + if (size < 16) { |
| 36 | + yield* insertionSort(arr, low, high); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // If depth limit reached, switch to heap sort |
| 41 | + if (depthLimit === 0) { |
| 42 | + yield* heapSortRange(arr, low, high); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // Use quick sort for larger arrays |
| 47 | + const pivotIndex = yield* partition(arr, low, high); |
| 48 | + |
| 49 | + // Recursively sort the two partitions |
| 50 | + yield* introSortRecursive(arr, low, pivotIndex - 1, depthLimit - 1); |
| 51 | + yield* introSortRecursive(arr, pivotIndex + 1, high, depthLimit - 1); |
| 52 | +} |
| 53 | + |
| 54 | +function* partition( |
| 55 | + arr: number[], |
| 56 | + low: number, |
| 57 | + high: number |
| 58 | +): Generator<ProgressIndicator, number, unknown> { |
| 59 | + // Choose middle element as pivot |
| 60 | + const pivotIndex = Math.floor((low + high) / 2); |
| 61 | + const pivot = arr[pivotIndex]; |
| 62 | + |
| 63 | + // Move pivot to end |
| 64 | + [arr[pivotIndex], arr[high]] = [arr[high], arr[pivotIndex]]; |
| 65 | + yield { access: [pivotIndex, high], sound: high }; |
| 66 | + |
| 67 | + let i = low - 1; |
| 68 | + |
| 69 | + for (let j = low; j < high; j++) { |
| 70 | + yield { access: [j], sound: j }; |
| 71 | + |
| 72 | + if (arr[j] <= pivot) { |
| 73 | + i++; |
| 74 | + if (i !== j) { |
| 75 | + [arr[i], arr[j]] = [arr[j], arr[i]]; |
| 76 | + yield { access: [i, j], sound: j }; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Move pivot to its correct position |
| 82 | + [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]]; |
| 83 | + yield { access: [i + 1, high], sound: high }; |
| 84 | + |
| 85 | + return i + 1; |
| 86 | +} |
| 87 | + |
| 88 | +function* insertionSort( |
| 89 | + arr: number[], |
| 90 | + low: number, |
| 91 | + high: number |
| 92 | +): SortingGenerator { |
| 93 | + for (let i = low + 1; i <= high; i++) { |
| 94 | + const key = arr[i]; |
| 95 | + let j = i - 1; |
| 96 | + |
| 97 | + yield { access: [i], sound: i }; |
| 98 | + |
| 99 | + while (j >= low && arr[j] > key) { |
| 100 | + yield { access: [j, j + 1], sound: j + 1 }; |
| 101 | + arr[j + 1] = arr[j]; |
| 102 | + j--; |
| 103 | + } |
| 104 | + |
| 105 | + if (j + 1 !== i) { |
| 106 | + arr[j + 1] = key; |
| 107 | + yield { access: [j + 1], sound: j + 1 }; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function* heapSortRange( |
| 113 | + arr: number[], |
| 114 | + low: number, |
| 115 | + high: number |
| 116 | +): SortingGenerator { |
| 117 | + const n = high - low + 1; |
| 118 | + |
| 119 | + // Build max heap |
| 120 | + for (let i = Math.floor(n / 2) - 1; i >= 0; i--) { |
| 121 | + yield* heapify(arr, low, n, i); |
| 122 | + } |
| 123 | + |
| 124 | + // Extract elements from heap one by one |
| 125 | + for (let i = n - 1; i > 0; i--) { |
| 126 | + // Move current root to end |
| 127 | + [arr[low], arr[low + i]] = [arr[low + i], arr[low]]; |
| 128 | + yield { access: [low, low + i], sound: low + i }; |
| 129 | + |
| 130 | + // Call heapify on the reduced heap |
| 131 | + yield* heapify(arr, low, i, 0); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +function* heapify( |
| 136 | + arr: number[], |
| 137 | + low: number, |
| 138 | + n: number, |
| 139 | + i: number |
| 140 | +): SortingGenerator { |
| 141 | + let largest = i; |
| 142 | + const left = 2 * i + 1; |
| 143 | + const right = 2 * i + 2; |
| 144 | + |
| 145 | + if (left < n && arr[low + left] > arr[low + largest]) { |
| 146 | + largest = left; |
| 147 | + } |
| 148 | + |
| 149 | + if (right < n && arr[low + right] > arr[low + largest]) { |
| 150 | + largest = right; |
| 151 | + } |
| 152 | + |
| 153 | + if (largest !== i) { |
| 154 | + [arr[low + i], arr[low + largest]] = [arr[low + largest], arr[low + i]]; |
| 155 | + yield { access: [low + i, low + largest], sound: low + largest }; |
| 156 | + |
| 157 | + yield* heapify(arr, low, n, largest); |
| 158 | + } |
| 159 | +} |
0 commit comments