|
| 1 | +package Sorts; |
| 2 | + |
| 3 | +import java.lang.Math; |
| 4 | +import java.util.Random; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author [Hemanth Kotagiri](https://github.com/hemanth-kotagiri) |
| 8 | + * @see [Tim Sort](https://en.wikipedia.org/wiki/Tim_sort) |
| 9 | + */ |
| 10 | + |
| 11 | +class TimSort { |
| 12 | + int array[]; |
| 13 | + int array_length; |
| 14 | + int RUN = 32; |
| 15 | + |
| 16 | + /** |
| 17 | + * @brief A constructor which takes in the array specified by the user. |
| 18 | + * @param array : Array given by the user. |
| 19 | + */ |
| 20 | + |
| 21 | + public TimSort(int[] array) { |
| 22 | + this.array = array; |
| 23 | + this.array_length = array.length; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @brief A constructor which takes in an array length and randomly |
| 28 | + * initializes an array. |
| 29 | + * @param array_length length given by the user. |
| 30 | + */ |
| 31 | + |
| 32 | + public TimSort(int array_length) { |
| 33 | + Random rand = new Random(); |
| 34 | + |
| 35 | + this.array_length = array_length; |
| 36 | + this.array = new int[this.array_length]; |
| 37 | + |
| 38 | + for (int i = 0; i < this.array_length; i++) { |
| 39 | + int random_number = rand.nextInt(1000); |
| 40 | + this.array[i] = random_number; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @brief A method to change the size of the run. |
| 46 | + * @param run : Value specified by the user to change the run. |
| 47 | + */ |
| 48 | + |
| 49 | + public void change_run(int run) { |
| 50 | + this.RUN = run; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief A default constructor when no parameters are given. |
| 55 | + * Initializes the array length to be 100. |
| 56 | + * Generates a random number array of size 100. |
| 57 | + */ |
| 58 | + |
| 59 | + public TimSort() { |
| 60 | + this.array_length = 100; |
| 61 | + this.array = new int[this.array_length]; |
| 62 | + |
| 63 | + Random rand = new Random(); |
| 64 | + for (int i = 0; i < this.array_length; i++) { |
| 65 | + int random_number = rand.nextInt(1000); |
| 66 | + this.array[i] = random_number; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Performs Insertion Sort Algorithm on given array with bounded |
| 72 | + * indices. |
| 73 | + * @param array: The array on which the algorithm is to be performed. |
| 74 | + * @param start_idx: The starting index from which the algorithm is to be |
| 75 | + * performed. |
| 76 | + * @param end_idx: The ending index at which the algorithm needs to stop |
| 77 | + * sorting. |
| 78 | + */ |
| 79 | + |
| 80 | + public void insertion_sort(int[] array, int start_idx, int end_idx) { |
| 81 | + for (int i = 0; i < array.length; i++) { |
| 82 | + int current_element = array[i]; |
| 83 | + int j = i - 1; |
| 84 | + while (j >= 0 && array[j] > current_element) { |
| 85 | + array[j + 1] = array[j]; |
| 86 | + j--; |
| 87 | + } |
| 88 | + array[j + 1] = current_element; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @brief A method to merge two runs(chunks of array). |
| 94 | + * @param array: The origin array which is to be sorted. |
| 95 | + * @param start: Starting index of the first run(chunk). |
| 96 | + * @param mid: The ending index of the first run(chunk). |
| 97 | + * @param end: Ending index of the second run(chunk). |
| 98 | + */ |
| 99 | + |
| 100 | + public void merge_runs(int array[], int start, int mid, int end) { |
| 101 | + |
| 102 | + int first_array_size = mid - start + 1, second_array_size = end - mid; |
| 103 | + int array1[] = new int[first_array_size], array2[] = new int[second_array_size]; |
| 104 | + int i = 0, j = 0, k = 0; |
| 105 | + |
| 106 | + // Building the two sub arrays from the array to merge later |
| 107 | + for (i = 0; i < first_array_size; i++) |
| 108 | + array1[i] = array[start + i]; |
| 109 | + for (i = 0; i < second_array_size; i++) |
| 110 | + array2[i] = array[mid + 1 + i]; |
| 111 | + |
| 112 | + i = 0; |
| 113 | + j = 0; |
| 114 | + k = start; |
| 115 | + |
| 116 | + while (i < first_array_size && j < second_array_size) { |
| 117 | + if (array1[i] <= array2[j]) { |
| 118 | + array[k] = array1[i]; |
| 119 | + i++; |
| 120 | + } else { |
| 121 | + array[k] = array2[j]; |
| 122 | + j++; |
| 123 | + } |
| 124 | + k++; |
| 125 | + } |
| 126 | + |
| 127 | + while (i < first_array_size) { |
| 128 | + array[k] = array1[i]; |
| 129 | + k++; |
| 130 | + i++; |
| 131 | + } |
| 132 | + |
| 133 | + while (j < second_array_size) { |
| 134 | + array[k] = array2[j]; |
| 135 | + k++; |
| 136 | + j++; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @brief Tim Sort Algorithm method. |
| 142 | + */ |
| 143 | + |
| 144 | + public void algorithm() { |
| 145 | + // Before Sorting |
| 146 | + System.out.println("Before sorting the array: "); |
| 147 | + this.showArrayElements(); |
| 148 | + System.out.println(); |
| 149 | + |
| 150 | + // Applying insertion sort on RUNS. |
| 151 | + for (int i = 0; i < this.array_length; i += this.RUN) |
| 152 | + this.insertion_sort(this.array, i, Math.min(i + this.RUN, (this.array_length - 1))); |
| 153 | + |
| 154 | + for (int split = this.RUN; split < this.array_length; split = 2 * split) { |
| 155 | + for (int start_idx = 0; start_idx < this.array_length; start_idx += 2 * split) { |
| 156 | + int mid = start_idx + split - 1; |
| 157 | + int end_idx = Math.min((start_idx + 2 * split - 1), (this.array_length - 1)); |
| 158 | + |
| 159 | + this.merge_runs(this.array, start_idx, mid, end_idx); |
| 160 | + } |
| 161 | + } |
| 162 | + // After sorting |
| 163 | + System.out.println("After sorting the array: "); |
| 164 | + this.showArrayElements(); |
| 165 | + System.out.println(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @brief A method to show the elements inside the array. |
| 170 | + */ |
| 171 | + |
| 172 | + public void showArrayElements() { |
| 173 | + for (int i = 0; i < this.array.length; i++) { |
| 174 | + System.out.print(this.array[i] + " "); |
| 175 | + } |
| 176 | + System.out.println(); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @brief A method to test the sorting algorithm |
| 181 | + */ |
| 182 | + |
| 183 | + static void test() { |
| 184 | + int[] array = { 4, 1, 3, 17, 12, 11, 8 }; |
| 185 | + TimSort sorterObj1 = new TimSort(); |
| 186 | + TimSort sorterObj2 = new TimSort(50); |
| 187 | + TimSort sorterObj3 = new TimSort(array); |
| 188 | + |
| 189 | + |
| 190 | + sorterObj1.algorithm(); |
| 191 | + sorterObj2.algorithm(); |
| 192 | + sorterObj3.algorithm(); |
| 193 | + |
| 194 | + // Testing the first array |
| 195 | + for(int i = 0; i < sorterObj1.array_length - 1; i++) { |
| 196 | + assert((sorterObj1.array[i] <= sorterObj1.array[i +1])) : "Array is not sorted"; |
| 197 | + } |
| 198 | + |
| 199 | + // Testing the second array. |
| 200 | + for(int i = 0; i < sorterObj2.array_length - 1; i++) { |
| 201 | + assert((sorterObj2.array[i] <= sorterObj2.array[i + 1])) : "Array is not sorted"; |
| 202 | + } |
| 203 | + |
| 204 | + // Testing the third array. |
| 205 | + for(int i = 0; i < sorterObj3.array_length - 1; i++) { |
| 206 | + assert((sorterObj3.array[i] <= sorterObj3.array[i + 1])) : "Array is not sorted"; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + public static void main(String[] args) { |
| 211 | + test(); |
| 212 | + } |
| 213 | +} |
0 commit comments