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