|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#define RUN 32 // Size of the run |
| 6 | + |
| 7 | +// Function to perform insertion sort on a subarray |
| 8 | +void insertionSort(int arr[], int left, int right) { |
| 9 | + for (int i = left + 1; i <= right; i++) { |
| 10 | + int key = arr[i]; // Element to be inserted |
| 11 | + int j = i - 1; |
| 12 | + |
| 13 | + // Move elements of arr[left..i-1] that are greater than key |
| 14 | + // to one position ahead of their current position |
| 15 | + while (j >= left && arr[j] > key) { |
| 16 | + arr[j + 1] = arr[j]; |
| 17 | + j--; |
| 18 | + } |
| 19 | + arr[j + 1] = key; // Place the key in its correct position |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Function to merge two sorted subarrays |
| 24 | +void merge(int arr[], int left, int mid, int right) { |
| 25 | + int i, j, k; |
| 26 | + int n1 = mid - left + 1; // Size of left subarray |
| 27 | + int n2 = right - mid; // Size of right subarray |
| 28 | + |
| 29 | + // Create temporary arrays for left and right subarrays |
| 30 | + int *L = (int*)malloc(n1 * sizeof(int)); |
| 31 | + int *R = (int*)malloc(n2 * sizeof(int)); |
| 32 | + |
| 33 | + // Copy data to temporary arrays L[] and R[] |
| 34 | + for (i = 0; i < n1; i++) |
| 35 | + L[i] = arr[left + i]; |
| 36 | + for (j = 0; j < n2; j++) |
| 37 | + R[j] = arr[mid + 1 + j]; |
| 38 | + |
| 39 | + // Merge the temporary arrays back into arr[left..right] |
| 40 | + i = 0; |
| 41 | + j = 0; |
| 42 | + k = left; |
| 43 | + |
| 44 | + // Merge while both subarrays have elements |
| 45 | + while (i < n1 && j < n2) { |
| 46 | + if (L[i] <= R[j]) { |
| 47 | + arr[k] = L[i]; // If the current element of L[] is smaller |
| 48 | + i++; |
| 49 | + } else { |
| 50 | + arr[k] = R[j]; // If the current element of R[] is smaller |
| 51 | + j++; |
| 52 | + } |
| 53 | + k++; |
| 54 | + } |
| 55 | + |
| 56 | + // Copy remaining elements of L[], if any |
| 57 | + while (i < n1) { |
| 58 | + arr[k] = L[i]; |
| 59 | + i++; |
| 60 | + k++; |
| 61 | + } |
| 62 | + |
| 63 | + // Copy remaining elements of R[], if any |
| 64 | + while (j < n2) { |
| 65 | + arr[k] = R[j]; |
| 66 | + j++; |
| 67 | + k++; |
| 68 | + } |
| 69 | + |
| 70 | + // Free the temporary arrays |
| 71 | + free(L); |
| 72 | + free(R); |
| 73 | +} |
| 74 | + |
| 75 | +// Main TimSort function |
| 76 | +void timSort(int arr[], int n) { |
| 77 | + // Step 1: Sort individual subarrays of size RUN |
| 78 | + for (int start = 0; start < n; start += RUN) { |
| 79 | + int end = (start + RUN - 1 < n - 1) ? (start + RUN - 1) : (n - 1); |
| 80 | + insertionSort(arr, start, end); // Sort the run using insertion sort |
| 81 | + } |
| 82 | + |
| 83 | + // Step 2: Merge the sorted runs |
| 84 | + for (int size = RUN; size < n; size *= 2) { // Double the size of the run |
| 85 | + for (int left = 0; left < n; left += 2 * size) { |
| 86 | + int mid = left + size - 1; // Ending index of the left subarray |
| 87 | + int right = ((left + 2 * size - 1) < (n - 1)) ? (left + 2 * size - 1) : (n - 1); // Ending index of the right subarray |
| 88 | + |
| 89 | + // Merge the two subarrays if valid |
| 90 | + if (mid < right) { |
| 91 | + merge(arr, left, mid, right); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Function to print the array |
| 98 | +void printArray(int arr[], int size) { |
| 99 | + for (int i = 0; i < size; i++) |
| 100 | + printf("%d ", arr[i]); // Print each element |
| 101 | + printf("\n"); // New line after printing the array |
| 102 | +} |
| 103 | + |
| 104 | +// Main function to test the TimSort implementation |
| 105 | +int main() { |
| 106 | + int arr[] = {5, 21, 7, 23, 19, 10, 1, 34, 0}; // Example array |
| 107 | + int n = sizeof(arr) / sizeof(arr[0]); // Calculate number of elements in the array |
| 108 | + |
| 109 | + printf("Original array: "); |
| 110 | + printArray(arr, n); // Print the original array |
| 111 | + |
| 112 | + timSort(arr, n); // Call TimSort on the array |
| 113 | + |
| 114 | + printf("Sorted array: "); |
| 115 | + printArray(arr, n); // Print the sorted array |
| 116 | + |
| 117 | + return 0; // End of program |
| 118 | +} |
0 commit comments