|
| 1 | +// Implementation of IntroSort (Introspective Sort) |
| 2 | +// Author: sgindeed |
| 3 | +// Description: |
| 4 | +// IntroSort is a hybrid sorting algorithm that starts with QuickSort, |
| 5 | +// switches to HeapSort when recursion depth exceeds a limit, and uses |
| 6 | +// InsertionSort for small arrays. It combines average-case speed of QuickSort |
| 7 | +// with the worst-case performance guarantee of HeapSort. |
| 8 | +// |
| 9 | +// Time Complexity: O(n log n) |
| 10 | +// Space Complexity: O(log n) due to recursion stack |
| 11 | +// |
| 12 | +// References: |
| 13 | +// - Musser, David R. "Introspective Sorting and Selection Algorithms." Software: Practice and Experience (1997) |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <math.h> |
| 18 | + |
| 19 | +#define SIZE 50 |
| 20 | +#define INSERTION_SORT_THRESHOLD 16 |
| 21 | + |
| 22 | +/* -------------------- Utility Functions -------------------- */ |
| 23 | + |
| 24 | +void swap(int *a, int *b) { |
| 25 | + int temp = *a; |
| 26 | + *a = *b; |
| 27 | + *b = temp; |
| 28 | +} |
| 29 | + |
| 30 | +/* -------------------- Insertion Sort -------------------- */ |
| 31 | + |
| 32 | +void insertionSort(int arr[], int left, int right) { |
| 33 | + for (int i = left + 1; i <= right; i++) { |
| 34 | + int key = arr[i]; |
| 35 | + int j = i - 1; |
| 36 | + while (j >= left && arr[j] > key) { |
| 37 | + arr[j + 1] = arr[j]; |
| 38 | + j--; |
| 39 | + } |
| 40 | + arr[j + 1] = key; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/* -------------------- Heap Sort -------------------- */ |
| 45 | + |
| 46 | +void heapify(int arr[], int n, int i, int start) { |
| 47 | + int largest = i; |
| 48 | + int left = 2 * (i - start) + 1 + start; |
| 49 | + int right = 2 * (i - start) + 2 + start; |
| 50 | + |
| 51 | + if (left < n && arr[left] > arr[largest]) |
| 52 | + largest = left; |
| 53 | + if (right < n && arr[right] > arr[largest]) |
| 54 | + largest = right; |
| 55 | + |
| 56 | + if (largest != i) { |
| 57 | + swap(&arr[i], &arr[largest]); |
| 58 | + heapify(arr, n, largest, start); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void heapSort(int arr[], int start, int end) { |
| 63 | + int n = end - start + 1; |
| 64 | + |
| 65 | + // Build heap (rearrange array) |
| 66 | + for (int i = start + n / 2 - 1; i >= start; i--) |
| 67 | + heapify(arr, end + 1, i, start); |
| 68 | + |
| 69 | + // Extract elements one by one |
| 70 | + for (int i = end; i >= start; i--) { |
| 71 | + swap(&arr[start], &arr[i]); |
| 72 | + heapify(arr, i, start, start); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/* -------------------- Quick Sort (used in IntroSort) -------------------- */ |
| 77 | + |
| 78 | +int partition(int arr[], int low, int high) { |
| 79 | + int pivot = arr[high]; |
| 80 | + int i = low - 1; |
| 81 | + |
| 82 | + for (int j = low; j < high; j++) { |
| 83 | + if (arr[j] <= pivot) { |
| 84 | + i++; |
| 85 | + swap(&arr[i], &arr[j]); |
| 86 | + } |
| 87 | + } |
| 88 | + swap(&arr[i + 1], &arr[high]); |
| 89 | + return (i + 1); |
| 90 | +} |
| 91 | + |
| 92 | +void introSortUtil(int arr[], int low, int high, int depthLimit) { |
| 93 | + int size = high - low + 1; |
| 94 | + |
| 95 | + // Use insertion sort for small arrays |
| 96 | + if (size < INSERTION_SORT_THRESHOLD) { |
| 97 | + insertionSort(arr, low, high); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // If depth limit is 0, use heap sort |
| 102 | + if (depthLimit == 0) { |
| 103 | + heapSort(arr, low, high); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + // Otherwise, use QuickSort |
| 108 | + int pivot = partition(arr, low, high); |
| 109 | + |
| 110 | + introSortUtil(arr, low, pivot - 1, depthLimit - 1); |
| 111 | + introSortUtil(arr, pivot + 1, high, depthLimit - 1); |
| 112 | +} |
| 113 | + |
| 114 | +/* -------------------- Main IntroSort Function -------------------- */ |
| 115 | + |
| 116 | +void introSort(int arr[], int n) { |
| 117 | + int depthLimit = 2 * log(n); |
| 118 | + introSortUtil(arr, 0, n - 1, depthLimit); |
| 119 | +} |
| 120 | + |
| 121 | +/* -------------------- Display Utility -------------------- */ |
| 122 | + |
| 123 | +void display(int arr[], int n) { |
| 124 | + for (int i = 0; i < n; i++) |
| 125 | + printf("%d ", arr[i]); |
| 126 | + printf("\n"); |
| 127 | +} |
| 128 | + |
| 129 | +/* -------------------- Driver Code -------------------- */ |
| 130 | + |
| 131 | +int main() { |
| 132 | + int arr[SIZE]; |
| 133 | + |
| 134 | + // Generate random numbers |
| 135 | + for (int i = 0; i < SIZE; i++) |
| 136 | + arr[i] = rand() % (SIZE << 1); // random numbers from 0 to 2*SIZE |
| 137 | + |
| 138 | + printf("Original array:\n"); |
| 139 | + display(arr, SIZE); |
| 140 | + |
| 141 | + introSort(arr, SIZE); |
| 142 | + |
| 143 | + printf("\nSorted array:\n"); |
| 144 | + display(arr, SIZE); |
| 145 | + |
| 146 | + return 0; |
| 147 | +} |
0 commit comments