From ff3e8f010d07f7b28bef8a01cd790c8076a9d2fa Mon Sep 17 00:00:00 2001 From: "hemanth@laptop_ubuntu" Date: Wed, 17 Feb 2021 19:55:31 +0530 Subject: [PATCH 01/20] Add Tim Sort implementation in Java --- Sorts/TimSort.java | 125 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Sorts/TimSort.java diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java new file mode 100644 index 000000000000..c237c4263382 --- /dev/null +++ b/Sorts/TimSort.java @@ -0,0 +1,125 @@ +package Sorts; + +import java.lang.Math; +import java.util.Random; + +class TimSort { + int array[]; + int array_length; + int RUN = 32; + + public TimSort(int[] array) { + this.array = array; + this.array_length = array.length; + } + + public TimSort(int array_length) { + Random rand = new Random(); + + this.array_length = array_length; + this.array = new int[this.array_length]; + + for (int i = 0; i < this.array_length; i++) { + int random_number = rand.nextInt(1000); + this.array[i] = random_number; + } + } + + public void change_run(int run) { + this.RUN = run; + } + + public TimSort() { + this.array_length = 100; + this.array = new int[this.array_length]; + + Random rand = new Random(); + for (int i = 0; i < this.array_length; i++) { + int random_number = rand.nextInt(1000); + this.array[i] = random_number; + } + } + + public void insertion_sort(int[] array, int start_idx, int end_idx) { + for (int i = 0; i < array.length; i++) { + int current_element = array[i]; + int j = i - 1; + while (j >= 0 && array[j] > current_element) { + array[j + 1] = array[j]; + j--; + } + array[j + 1] = current_element; + } + } + + public void merge_runs(int array[], int start, int mid, int end) { + + int first_array_size = mid - start + 1, second_array_size = end - mid; + int array1[] = new int[first_array_size], array2[] = new int[second_array_size]; + int i = 0, j = 0, k = 0; + + // Building the two sub arrays from the array to merge later + for (i = 0; i < first_array_size; i++) + array1[i] = array[start + i]; + for (i = 0; i < second_array_size; i++) + array2[i] = array[mid + 1 + i]; + + i = 0; + j = 0; + k = start; + + while (i < first_array_size && j < second_array_size) { + if (array1[i] <= array2[j]) { + array[k] = array1[i]; + i++; + } else { + array[k] = array2[j]; + j++; + } + k++; + } + + while (i < first_array_size) { + array[k] = array1[i]; + k++; + i++; + } + + while (j < second_array_size) { + array[k] = array2[j]; + k++; + j++; + } + } + + public void algorithm() { + // Before Sorting + System.out.println("Before sorting the array: "); + this.showArrayElements(); + System.out.println(); + + // Applying insertion sort on RUNS. + for (int i = 0; i < this.array_length; i += this.RUN) + this.insertion_sort(this.array, i, Math.min(i + this.RUN, (this.array_length - 1))); + + for (int split = this.RUN; split < this.array_length; split = 2 * split) { + for (int start_idx = 0; start_idx < this.array_length; start_idx += 2 * split) { + int mid = start_idx + split - 1; + int end_idx = Math.min((start_idx + 2 * split - 1), (this.array_length - 1)); + + this.merge_runs(this.array, start_idx, mid, end_idx); + } + } + // After sorting + System.out.println("After sorting the array: "); + this.showArrayElements(); + System.out.println(); + } + + public void showArrayElements() { + for (int i = 0; i < this.array.length; i++) { + System.out.print(this.array[i] + " "); + } + System.out.println(); + } +} From 120aee877ad4561187dbc20d2b0ac4463f872082 Mon Sep 17 00:00:00 2001 From: "hemanth@wsl2" Date: Sun, 21 Feb 2021 16:14:24 +0530 Subject: [PATCH 02/20] Add comments and complete implementation of TimSort Algorithm --- Sorts/TimSort.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index c237c4263382..64ce57998b58 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -3,16 +3,29 @@ import java.lang.Math; import java.util.Random; +/** + * @author Hemanth Kotagiri (https://github.com/hemanth-kotagiri) + * @see SortAlgorithm + */ + +// Tim Sort: https://en.wikipedia.org/wiki/Tim_sort class TimSort { int array[]; int array_length; int RUN = 32; + /** + * @param array the array given by the user + */ + public TimSort(int[] array) { this.array = array; this.array_length = array.length; } + /** + * @param array_length the array_length given by the user to generate random array of that length + */ public TimSort(int array_length) { Random rand = new Random(); @@ -25,10 +38,15 @@ public TimSort(int array_length) { } } + /** + * @param run change size of the run + */ + public void change_run(int run) { this.RUN = run; } + // Constructor when no argument is given. public TimSort() { this.array_length = 100; this.array = new int[this.array_length]; @@ -40,6 +58,7 @@ public TimSort() { } } + // Insertion sort algorithm public void insertion_sort(int[] array, int start_idx, int end_idx) { for (int i = 0; i < array.length; i++) { int current_element = array[i]; @@ -92,6 +111,7 @@ public void merge_runs(int array[], int start, int mid, int end) { } } + // Tim Sort Algorithm public void algorithm() { // Before Sorting System.out.println("Before sorting the array: "); From e974ddf359e16be927c9a89e36fe11db38f570f5 Mon Sep 17 00:00:00 2001 From: Hemanth Date: Sat, 27 Feb 2021 16:53:47 +0530 Subject: [PATCH 03/20] Add better docs --- Sorts/TimSort.java | 50 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 64ce57998b58..5b997ba6efb1 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -8,14 +8,15 @@ * @see SortAlgorithm */ -// Tim Sort: https://en.wikipedia.org/wiki/Tim_sort + // Wikipedia: https://en.wikipedia.org/wiki/Tim_sort class TimSort { int array[]; int array_length; int RUN = 32; /** - * @param array the array given by the user + * A constructor which takes in the array specified by the user. + * @param array : Array given by the user. */ public TimSort(int[] array) { @@ -24,8 +25,11 @@ public TimSort(int[] array) { } /** - * @param array_length the array_length given by the user to generate random array of that length - */ + * A constructor which takes in an array length and randomly initializes an + * array. + * @param array_length length given by the + */ + public TimSort(int array_length) { Random rand = new Random(); @@ -38,15 +42,20 @@ public TimSort(int array_length) { } } - /** - * @param run change size of the run + /** A method to change the size of the run. + * @param run : Value specified by the user to change the run. */ public void change_run(int run) { this.RUN = run; } - // Constructor when no argument is given. + /** + * A default constructor when no parameters are given. + * Initializes the array length to be 100. + * Generates a random number array of size 100. + */ + public TimSort() { this.array_length = 100; this.array = new int[this.array_length]; @@ -58,7 +67,15 @@ public TimSort() { } } - // Insertion sort algorithm + /** + * Performs Insertion Sort Algorithm on given array with bounded indices. + * @param array : The array on which the algorithm is to be performed. + * @param start_idx : The starting index from which the algorithm is to be + * performed. + * @param end_idx : The ending index at which the algorithm needs to stop + * sorting. + */ + public void insertion_sort(int[] array, int start_idx, int end_idx) { for (int i = 0; i < array.length; i++) { int current_element = array[i]; @@ -71,6 +88,14 @@ public void insertion_sort(int[] array, int start_idx, int end_idx) { } } + /** + * A method to merge two runs(chunks of array). + * @param array : The origin array which is to be sorted. + * @param start : Starting index of the first run(chunk). + * @param mid : The ending index of the first run(chunk). + * @param end : Ending index of the second run(chunk). + */ + public void merge_runs(int array[], int start, int mid, int end) { int first_array_size = mid - start + 1, second_array_size = end - mid; @@ -111,7 +136,10 @@ public void merge_runs(int array[], int start, int mid, int end) { } } - // Tim Sort Algorithm + /** + * Tim Sort Algorithm method. + */ + public void algorithm() { // Before Sorting System.out.println("Before sorting the array: "); @@ -136,6 +164,10 @@ public void algorithm() { System.out.println(); } + /** + * A method to show the elements inside the array. + */ + public void showArrayElements() { for (int i = 0; i < this.array.length; i++) { System.out.print(this.array[i] + " "); From e9458db669ad4fc0899b83bc97090eb310b8e6eb Mon Sep 17 00:00:00 2001 From: Hemanth Date: Tue, 9 Mar 2021 22:33:34 +0530 Subject: [PATCH 04/20] Add @brief's and test method --- Sorts/TimSort.java | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 5b997ba6efb1..3fc28a6d0b82 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -4,18 +4,18 @@ import java.util.Random; /** - * @author Hemanth Kotagiri (https://github.com/hemanth-kotagiri) + * @author [Hemanth Kotagiri](https://github.com/hemanth-kotagiri) * @see SortAlgorithm */ - // Wikipedia: https://en.wikipedia.org/wiki/Tim_sort + // [Wikipedia](https://en.wikipedia.org/wiki/Tim_sort) class TimSort { int array[]; int array_length; int RUN = 32; /** - * A constructor which takes in the array specified by the user. + * @brief A constructor which takes in the array specified by the user. * @param array : Array given by the user. */ @@ -25,9 +25,9 @@ public TimSort(int[] array) { } /** - * A constructor which takes in an array length and randomly initializes an - * array. - * @param array_length length given by the + * @brief A constructor which takes in an array length and randomly + * initializes an array. + * @param array_length length given by the user. */ public TimSort(int array_length) { @@ -42,7 +42,8 @@ public TimSort(int array_length) { } } - /** A method to change the size of the run. + /** + * @brief A method to change the size of the run. * @param run : Value specified by the user to change the run. */ @@ -51,7 +52,7 @@ public void change_run(int run) { } /** - * A default constructor when no parameters are given. + * @brief A default constructor when no parameters are given. * Initializes the array length to be 100. * Generates a random number array of size 100. */ @@ -68,7 +69,8 @@ public TimSort() { } /** - * Performs Insertion Sort Algorithm on given array with bounded indices. + * @brief Performs Insertion Sort Algorithm on given array with bounded + * indices. * @param array : The array on which the algorithm is to be performed. * @param start_idx : The starting index from which the algorithm is to be * performed. @@ -89,7 +91,7 @@ public void insertion_sort(int[] array, int start_idx, int end_idx) { } /** - * A method to merge two runs(chunks of array). + * @brief A method to merge two runs(chunks of array). * @param array : The origin array which is to be sorted. * @param start : Starting index of the first run(chunk). * @param mid : The ending index of the first run(chunk). @@ -137,7 +139,7 @@ public void merge_runs(int array[], int start, int mid, int end) { } /** - * Tim Sort Algorithm method. + * @brief Tim Sort Algorithm method. */ public void algorithm() { @@ -165,7 +167,7 @@ public void algorithm() { } /** - * A method to show the elements inside the array. + * @brief A method to show the elements inside the array. */ public void showArrayElements() { @@ -175,3 +177,16 @@ public void showArrayElements() { System.out.println(); } } + +class Test { + public static void main(String[] args) { + TimSort sorterObj1 = new TimSort(); + TimSort sorterObj2 = new TimSort(50); + TimSort sorterObj3 = new TimSort([4, 1, 3, 17, 12, 11, 8]); + + + sorterObj1.algorithm(); + sorterObj2.algorithm(); + sorterObj3.algorithm(); + } +} From 75b4a8e9d5c8cafc8248a3bb6fe7c4e94959031d Mon Sep 17 00:00:00 2001 From: Hemanth Date: Tue, 9 Mar 2021 22:43:35 +0530 Subject: [PATCH 05/20] Fix errors --- Sorts/TimSort.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 3fc28a6d0b82..57bd835bd2d1 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -176,13 +176,16 @@ public void showArrayElements() { } System.out.println(); } + public static void main(String[] args) { + } } class Test { public static void main(String[] args) { + int[] array = { 4, 1, 3, 17, 12, 11, 8 }; TimSort sorterObj1 = new TimSort(); TimSort sorterObj2 = new TimSort(50); - TimSort sorterObj3 = new TimSort([4, 1, 3, 17, 12, 11, 8]); + TimSort sorterObj3 = new TimSort(array); sorterObj1.algorithm(); From 0a868372f8a94627a7ff86e43bea1e1ef7b0a4b1 Mon Sep 17 00:00:00 2001 From: Hemanth Date: Wed, 10 Mar 2021 10:43:02 +0530 Subject: [PATCH 06/20] add Test method --- Sorts/TimSort.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 57bd835bd2d1..82429f42bbbc 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -5,10 +5,10 @@ /** * @author [Hemanth Kotagiri](https://github.com/hemanth-kotagiri) - * @see SortAlgorithm + * @see [Tim Sort](https://en.wikipedia.org/wiki/Tim_sort) */ - // [Wikipedia](https://en.wikipedia.org/wiki/Tim_sort) + // class TimSort { int array[]; int array_length; @@ -176,12 +176,12 @@ public void showArrayElements() { } System.out.println(); } - public static void main(String[] args) { - } -} -class Test { - public static void main(String[] args) { + /** + * @brief A method to test the sorting algorithm + */ + + static void test() { int[] array = { 4, 1, 3, 17, 12, 11, 8 }; TimSort sorterObj1 = new TimSort(); TimSort sorterObj2 = new TimSort(50); @@ -192,4 +192,9 @@ public static void main(String[] args) { sorterObj2.algorithm(); sorterObj3.algorithm(); } + + public static void main(String[] args) { + test(); + } } + From 327aea06227f156a3864b76700228cfa791e84d6 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:55:40 +0530 Subject: [PATCH 07/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 82429f42bbbc..d52adfc25b22 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -8,7 +8,6 @@ * @see [Tim Sort](https://en.wikipedia.org/wiki/Tim_sort) */ - // class TimSort { int array[]; int array_length; @@ -197,4 +196,3 @@ public static void main(String[] args) { test(); } } - From e673418fe4d1e2a49ef5a832d7ab763822391904 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:55:47 +0530 Subject: [PATCH 08/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index d52adfc25b22..03030e68d63a 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -70,7 +70,7 @@ public TimSort() { /** * @brief Performs Insertion Sort Algorithm on given array with bounded * indices. - * @param array : The array on which the algorithm is to be performed. + * @param array: The array on which the algorithm is to be performed. * @param start_idx : The starting index from which the algorithm is to be * performed. * @param end_idx : The ending index at which the algorithm needs to stop From f09cd0d907b84dae60006c20d522d7d785a2180c Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:55:56 +0530 Subject: [PATCH 09/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 03030e68d63a..8369da34f7b1 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -71,7 +71,7 @@ public TimSort() { * @brief Performs Insertion Sort Algorithm on given array with bounded * indices. * @param array: The array on which the algorithm is to be performed. - * @param start_idx : The starting index from which the algorithm is to be + * @param start_idx: The starting index from which the algorithm is to be * performed. * @param end_idx : The ending index at which the algorithm needs to stop * sorting. From 40dc8f80ac4004d77a20a4851dfc2952914da305 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:56:02 +0530 Subject: [PATCH 10/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 8369da34f7b1..d81116a9c670 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -73,7 +73,7 @@ public TimSort() { * @param array: The array on which the algorithm is to be performed. * @param start_idx: The starting index from which the algorithm is to be * performed. - * @param end_idx : The ending index at which the algorithm needs to stop + * @param end_idx: The ending index at which the algorithm needs to stop * sorting. */ From 324cb4e3f2e7779c65fa127d3aae06289514ccd9 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:56:08 +0530 Subject: [PATCH 11/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index d81116a9c670..3f7506573b60 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -91,7 +91,7 @@ public void insertion_sort(int[] array, int start_idx, int end_idx) { /** * @brief A method to merge two runs(chunks of array). - * @param array : The origin array which is to be sorted. + * @param array: The origin array which is to be sorted. * @param start : Starting index of the first run(chunk). * @param mid : The ending index of the first run(chunk). * @param end : Ending index of the second run(chunk). From e29cb48894f454908fe1b7dfceb4ed90b03ccd6d Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:56:13 +0530 Subject: [PATCH 12/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 3f7506573b60..e3fa7c3f1fc8 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -92,7 +92,7 @@ public void insertion_sort(int[] array, int start_idx, int end_idx) { /** * @brief A method to merge two runs(chunks of array). * @param array: The origin array which is to be sorted. - * @param start : Starting index of the first run(chunk). + * @param start: Starting index of the first run(chunk). * @param mid : The ending index of the first run(chunk). * @param end : Ending index of the second run(chunk). */ From dd8ad4b18316298065566d536a360b0254a9cb96 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:56:23 +0530 Subject: [PATCH 13/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index e3fa7c3f1fc8..0883d3245ca4 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -93,7 +93,7 @@ public void insertion_sort(int[] array, int start_idx, int end_idx) { * @brief A method to merge two runs(chunks of array). * @param array: The origin array which is to be sorted. * @param start: Starting index of the first run(chunk). - * @param mid : The ending index of the first run(chunk). + * @param mid: The ending index of the first run(chunk). * @param end : Ending index of the second run(chunk). */ From d1660bfcb4bea6c839121d225464758a35a6e31b Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Wed, 10 Mar 2021 10:56:29 +0530 Subject: [PATCH 14/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 0883d3245ca4..842f8b171658 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -94,7 +94,7 @@ public void insertion_sort(int[] array, int start_idx, int end_idx) { * @param array: The origin array which is to be sorted. * @param start: Starting index of the first run(chunk). * @param mid: The ending index of the first run(chunk). - * @param end : Ending index of the second run(chunk). + * @param end: Ending index of the second run(chunk). */ public void merge_runs(int array[], int start, int mid, int end) { From 51e56271512b1c2b869a69d8b8b397362393afe4 Mon Sep 17 00:00:00 2001 From: "Hemanth@Arch" Date: Wed, 21 Apr 2021 11:26:12 +0530 Subject: [PATCH 15/20] Add tests --- Sorts/TimSort.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 82429f42bbbc..22473b8ffff1 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -191,6 +191,21 @@ static void test() { sorterObj1.algorithm(); sorterObj2.algorithm(); sorterObj3.algorithm(); + + // Testing the first array + for(int i = 1; i < sorterObj1.array_length; i++) { + assert(!(sorterObj1.array[i] < sorterObj1.array[i - 1])) : "Array is not sorted"; + } + + // Testing the second array. + for(int i = 1; i < sorterObj2.array_length; i++) { + assert(!(sorterObj2.array[i] < sorterObj2.array[i - 1])) : "Array is not sorted"; + } + + // Testing the third array. + for(int i = 1; i < sorterObj3.array_length; i++) { + assert(!(sorterObj3.array[i] < sorterObj3.array[i - 1])) : "Array is not sorted"; + } } public static void main(String[] args) { From ed665f11911b86fd669365d83efa7369433e4f75 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Fri, 23 Apr 2021 00:02:02 +0530 Subject: [PATCH 16/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 8e812977d920..68391ea84a0d 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -192,8 +192,8 @@ static void test() { sorterObj3.algorithm(); // Testing the first array - for(int i = 1; i < sorterObj1.array_length; i++) { - assert(!(sorterObj1.array[i] < sorterObj1.array[i - 1])) : "Array is not sorted"; + for(int i = 0; i < sorterObj1.array_length - 1; i++) { + assert((sorterObj1.array[i] < sorterObj1.array[i +1])) : "Array is not sorted"; } // Testing the second array. From 179c84a5983c8689dea116fbccffe5c7129d9115 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Fri, 23 Apr 2021 16:15:39 +0530 Subject: [PATCH 17/20] Update Sorts/TimSort.java --- Sorts/TimSort.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 68391ea84a0d..befa77196aea 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -197,13 +197,13 @@ static void test() { } // Testing the second array. - for(int i = 1; i < sorterObj2.array_length; i++) { - assert(!(sorterObj2.array[i] < sorterObj2.array[i - 1])) : "Array is not sorted"; + for(int i = 0; i < sorterObj2.array_length - 1; i++) { + assert((sorterObj2.array[i] < sorterObj2.array[i + 1])) : "Array is not sorted"; } // Testing the third array. - for(int i = 1; i < sorterObj3.array_length; i++) { - assert(!(sorterObj3.array[i] < sorterObj3.array[i - 1])) : "Array is not sorted"; + for(int i = 0; i < sorterObj3.array_length - 1; i++) { + assert((sorterObj3.array[i] < sorterObj3.array[i + 1])) : "Array is not sorted"; } } From 0e81e1d93ee604ffd26548238b7b2dce34fbb217 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Fri, 23 Apr 2021 22:30:00 +0530 Subject: [PATCH 18/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index befa77196aea..088ed698f687 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -193,7 +193,7 @@ static void test() { // Testing the first array for(int i = 0; i < sorterObj1.array_length - 1; i++) { - assert((sorterObj1.array[i] < sorterObj1.array[i +1])) : "Array is not sorted"; + assert((sorterObj1.array[i] <= sorterObj1.array[i +1])) : "Array is not sorted"; } // Testing the second array. From d76e46d256f7fc78f85e6870db83c63f44831c03 Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Fri, 23 Apr 2021 22:30:07 +0530 Subject: [PATCH 19/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index 088ed698f687..ac0ad3cc49ba 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -203,7 +203,7 @@ static void test() { // Testing the third array. for(int i = 0; i < sorterObj3.array_length - 1; i++) { - assert((sorterObj3.array[i] < sorterObj3.array[i + 1])) : "Array is not sorted"; + assert((sorterObj3.array[i] <= sorterObj3.array[i + 1])) : "Array is not sorted"; } } From 4d7862fba478619ace24e79a9699afdb6666a32a Mon Sep 17 00:00:00 2001 From: Hemanth Kotagiri Date: Fri, 23 Apr 2021 22:36:20 +0530 Subject: [PATCH 20/20] Update Sorts/TimSort.java Co-authored-by: Ayaan Khan --- Sorts/TimSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/TimSort.java b/Sorts/TimSort.java index ac0ad3cc49ba..37efa5943e01 100644 --- a/Sorts/TimSort.java +++ b/Sorts/TimSort.java @@ -198,7 +198,7 @@ static void test() { // Testing the second array. for(int i = 0; i < sorterObj2.array_length - 1; i++) { - assert((sorterObj2.array[i] < sorterObj2.array[i + 1])) : "Array is not sorted"; + assert((sorterObj2.array[i] <= sorterObj2.array[i + 1])) : "Array is not sorted"; } // Testing the third array.