|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +// ---------------------------- Insertion Sort ---------------------------- |
| 5 | +void insertionSort(vector<int>& arr, int left, int right) { |
| 6 | + for (int i = left + 1; i <= right; i++) { |
| 7 | + int key = arr[i]; |
| 8 | + int j = i - 1; |
| 9 | + while (j >= left && arr[j] > key) { |
| 10 | + arr[j + 1] = arr[j]; |
| 11 | + j--; |
| 12 | + } |
| 13 | + arr[j + 1] = key; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +// ---------------------------- Heap Sort ---------------------------- |
| 18 | +void heapify(vector<int>& arr, int n, int i) { |
| 19 | + int largest = i; |
| 20 | + int left = 2 * i + 1; |
| 21 | + int right = 2 * i + 2; |
| 22 | + |
| 23 | + if (left < n && arr[left] > arr[largest]) |
| 24 | + largest = left; |
| 25 | + |
| 26 | + if (right < n && arr[right] > arr[largest]) |
| 27 | + largest = right; |
| 28 | + |
| 29 | + if (largest != i) { |
| 30 | + swap(arr[i], arr[largest]); |
| 31 | + heapify(arr, n, largest); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void heapSort(vector<int>& arr, int begin, int end) { |
| 36 | + int n = end - begin + 1; |
| 37 | + for (int i = n / 2 - 1; i >= 0; i--) { |
| 38 | + heapify(arr, n, i); |
| 39 | + } |
| 40 | + for (int i = n - 1; i >= 0; i--) { |
| 41 | + swap(arr[0], arr[i]); |
| 42 | + heapify(arr, i, 0); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// ---------------------------- Quick Sort Partition ---------------------------- |
| 47 | +int partition(vector<int>& arr, int low, int high) { |
| 48 | + int pivot = arr[high]; |
| 49 | + int i = low - 1; |
| 50 | + for (int j = low; j < high; j++) { |
| 51 | + if (arr[j] <= pivot) { |
| 52 | + i++; |
| 53 | + swap(arr[i], arr[j]); |
| 54 | + } |
| 55 | + } |
| 56 | + swap(arr[i + 1], arr[high]); |
| 57 | + return i + 1; |
| 58 | +} |
| 59 | + |
| 60 | +// ---------------------------- IntroSort Utility ---------------------------- |
| 61 | +void introSortUtil(vector<int>& arr, int begin, int end, int depthLimit) { |
| 62 | + int size = end - begin + 1; |
| 63 | + |
| 64 | + // Use InsertionSort for small partitions |
| 65 | + if (size < 16) { |
| 66 | + insertionSort(arr, begin, end); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // If recursion depth limit is zero, switch to HeapSort |
| 71 | + if (depthLimit == 0) { |
| 72 | + heapSort(arr, begin, end); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + int pivot = partition(arr, begin, end); |
| 77 | + introSortUtil(arr, begin, pivot - 1, depthLimit - 1); |
| 78 | + introSortUtil(arr, pivot + 1, end, depthLimit - 1); |
| 79 | +} |
| 80 | + |
| 81 | +// ---------------------------- IntroSort Driver ---------------------------- |
| 82 | +void introSort(vector<int>& arr) { |
| 83 | + int n = arr.size(); |
| 84 | + int depthLimit = 2 * log(n); |
| 85 | + introSortUtil(arr, 0, n - 1, depthLimit); |
| 86 | +} |
| 87 | + |
| 88 | +// ---------------------------- Testing ---------------------------- |
| 89 | +int main() { |
| 90 | + vector<int> arr = {10, 7, 8, 9, 1, 5, 20, 15, 3, 2}; |
| 91 | + |
| 92 | + cout << "Original array: "; |
| 93 | + for (int x : arr) cout << x << " "; |
| 94 | + cout << "\n"; |
| 95 | + |
| 96 | + introSort(arr); |
| 97 | + |
| 98 | + cout << "Sorted array: "; |
| 99 | + for (int x : arr) cout << x << " "; |
| 100 | + cout << "\n"; |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +/* |
| 106 | +---------------------------- Complexity ---------------------------- |
| 107 | +Time Complexity: |
| 108 | +- Best Case: O(n log n) (QuickSort dominates) |
| 109 | +- Average Case: O(n log n) |
| 110 | +- Worst Case: O(n log n) (due to HeapSort fallback) |
| 111 | +
|
| 112 | +Space Complexity: |
| 113 | +- O(log n) for recursion stack (QuickSort) |
| 114 | +*/ |
0 commit comments