|
| 1 | + |
| 2 | +class GFG |
| 3 | +{ |
| 4 | + |
| 5 | + static int MIN_MERGE = 32; |
| 6 | + |
| 7 | + public static int minRunLength(int n) |
| 8 | + { |
| 9 | + assert n >= 0; |
| 10 | + |
| 11 | + // Becomes 1 if any 1 bits are shifted off |
| 12 | + int r = 0; |
| 13 | + while (n >= MIN_MERGE) |
| 14 | + { |
| 15 | + r |= (n & 1); |
| 16 | + n >>= 1; |
| 17 | + } |
| 18 | + return n + r; |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | + public static void insertionSort(int[] arr, int left, |
| 23 | + int right) |
| 24 | + { |
| 25 | + for (int i = left + 1; i <= right; i++) |
| 26 | + { |
| 27 | + int temp = arr[i]; |
| 28 | + int j = i - 1; |
| 29 | + while (j >= left && arr[j] > temp) |
| 30 | + { |
| 31 | + arr[j + 1] = arr[j]; |
| 32 | + j--; |
| 33 | + } |
| 34 | + arr[j + 1] = temp; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Merge function merges the sorted runs |
| 39 | + public static void merge(int[] arr, int l, |
| 40 | + int m, int r) |
| 41 | + { |
| 42 | + // Original array is broken in two parts |
| 43 | + // left and right array |
| 44 | + int len1 = m - l + 1, len2 = r - m; |
| 45 | + int[] left = new int[len1]; |
| 46 | + int[] right = new int[len2]; |
| 47 | + for (int x = 0; x < len1; x++) |
| 48 | + { |
| 49 | + left[x] = arr[l + x]; |
| 50 | + } |
| 51 | + for (int x = 0; x < len2; x++) |
| 52 | + { |
| 53 | + right[x] = arr[m + 1 + x]; |
| 54 | + } |
| 55 | + |
| 56 | + int i = 0; |
| 57 | + int j = 0; |
| 58 | + int k = l; |
| 59 | + |
| 60 | + while (i < len1 && j < len2) |
| 61 | + { |
| 62 | + if (left[i] <= right[j]) |
| 63 | + { |
| 64 | + arr[k] = left[i]; |
| 65 | + i++; |
| 66 | + } |
| 67 | + else { |
| 68 | + arr[k] = right[j]; |
| 69 | + j++; |
| 70 | + } |
| 71 | + k++; |
| 72 | + } |
| 73 | + |
| 74 | + while (i < len1) |
| 75 | + { |
| 76 | + arr[k] = left[i]; |
| 77 | + k++; |
| 78 | + i++; |
| 79 | + } |
| 80 | + |
| 81 | + while (j < len2) |
| 82 | + { |
| 83 | + arr[k] = right[j]; |
| 84 | + k++; |
| 85 | + j++; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static void timSort(int[] arr, int n) |
| 90 | + { |
| 91 | + int minRun = minRunLength(MIN_MERGE); |
| 92 | + |
| 93 | + // Sort individual subarrays of size RUN |
| 94 | + for (int i = 0; i < n; i += minRun) |
| 95 | + { |
| 96 | + insertionSort(arr, i, |
| 97 | + Math.min((i + MIN_MERGE - 1), (n - 1))); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + for (int size = minRun; size < n; size = 2 * size) |
| 102 | + { |
| 103 | + for (int left = 0; left < n; |
| 104 | + left += 2 * size) |
| 105 | + { |
| 106 | + |
| 107 | + int mid = left + size - 1; |
| 108 | + int right = Math.min((left + 2 * size - 1), |
| 109 | + (n - 1)); |
| 110 | + |
| 111 | + if(mid < right) |
| 112 | + merge(arr, left, mid, right); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Utility function to print the Array |
| 118 | + public static void printArray(int[] arr, int n) |
| 119 | + { |
| 120 | + for (int i = 0; i < n; i++) { |
| 121 | + System.out.print(arr[i] + " "); |
| 122 | + } |
| 123 | + System.out.print("\n"); |
| 124 | + } |
| 125 | + |
| 126 | + // Driver code |
| 127 | + public static void main(String[] args) |
| 128 | + { |
| 129 | + int[] arr = { -2, 7, 15, -14, 0, 15, 0, 7, |
| 130 | + -7, -4, -13, 5, 8, -14, 12 }; |
| 131 | + int n = arr.length; |
| 132 | + System.out.println("Given Array is"); |
| 133 | + printArray(arr, n); |
| 134 | + |
| 135 | + timSort(arr, n); |
| 136 | + |
| 137 | + System.out.println("After Sorting Array is"); |
| 138 | + printArray(arr, n); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | + |
0 commit comments