Repository: TheAlgorithms/Java
Pull request #3774 (2022-11-19 17:00:46)

MergeSort: Simplify merge function
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/MergeSort.java b/src/main/java/com/thealgorithms/sorts/MergeSort.java
index b62e32b0..7c9bf9eb 100644
--- a/src/main/java/com/thealgorithms/sorts/MergeSort.java
+++ b/src/main/java/com/thealgorithms/sorts/MergeSort.java
@@ -1,5 +1,7 @@
 package com.thealgorithms.sorts;
 
+import static com.thealgorithms.sorts.SortUtils.less;
+
 /**
  * Generic merge sort algorithm.
  *
@@ -7,6 +9,9 @@ package com.thealgorithms.sorts;
  */
 class MergeSort implements SortAlgorithm {
 
+    @SuppressWarnings("rawtypes")
+    private static Comparable[] aux;
+
     /**
      * Generic merge sort algorithm implements.
      *
@@ -16,6 +21,7 @@ class MergeSort implements SortAlgorithm {
      */
     @Override
     public <T extends Comparable<T>> T[] sort(T[] unsorted) {
+        aux = new Comparable[unsorted.length];
         doSort(unsorted, 0, unsorted.length - 1);
         return unsorted;
     }
@@ -25,11 +31,7 @@ class MergeSort implements SortAlgorithm {
      * @param left the first index of the array.
      * @param right the last index of the array.
      */
-    private static <T extends Comparable<T>> void doSort(
-        T[] arr,
-        int left,
-        int right
-    ) {
+    private static <T extends Comparable<T>> void doSort(T[] arr, int left, int right) {
         if (left < right) {
             int mid = (left + right) >>> 1;
             doSort(arr, left, mid);
@@ -47,54 +49,21 @@ class MergeSort implements SortAlgorithm {
      * @param right the last index of the array merges two parts of an array in
      * increasing order.
      */
-    private static <T extends Comparable<T>> void merge(
-        T[] arr,
-        int left,
-        int mid,
-        int right
-    ) {
-        int length = right - left + 1;
-        @SuppressWarnings("unchecked")
-        T[] temp = (T[]) new Comparable[length];
-        int i = left;
-        int j = mid + 1;
-        int k = 0;
+    @SuppressWarnings("unchecked")
+    private static <T extends Comparable<T>> void merge(T[] arr, int left, int mid, int right) {
+        int i = left, j = mid + 1;
+        System.arraycopy(arr, left, aux, left, right + 1 - left);
 
-        while (i <= mid && j <= right) {
-            if (arr[i].compareTo(arr[j]) <= 0) {
-                temp[k++] = arr[i++];
+        for (int k = left; k <= right; k++) {
+            if (j > right) {
+                arr[k] = (T) aux[i++];
+            } else if (i > mid) {
+                arr[k] = (T) aux[j++];
+            } else if (less(aux[j], aux[i])) {
+                arr[k] = (T) aux[j++];
             } else {
-                temp[k++] = arr[j++];
+                arr[k] = (T) aux[i++];
             }
         }
-
-        while (i <= mid) {
-            temp[k++] = arr[i++];
-        }
-
-        while (j <= right) {
-            temp[k++] = arr[j++];
-        }
-
-        System.arraycopy(temp, 0, arr, left, length);
-    }
-
-    /**
-     * Driver code
-     */
-    public static void main(String[] args) {
-        MergeSort mergeSort = new MergeSort();
-
-        Integer[] arr = { 4, 23, 6, 78, 1, 54, 231, 9, 12 };
-        mergeSort.sort(arr);
-        for (int i = 0; i < arr.length - 1; ++i) {
-            assert arr[i] <= arr[i + 1];
-        }
-
-        String[] stringArray = { "c", "a", "e", "b", "d" };
-        mergeSort.sort(stringArray);
-        for (int i = 0; i < stringArray.length - 1; ++i) {
-            assert arr[i].compareTo(arr[i + 1]) <= 0;
-        }
     }
 }
diff --git a/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java b/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java
index aad511c7..2e2561e8 100644
--- a/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java
+++ b/src/main/java/com/thealgorithms/sorts/SortUtilsRandomGenerator.java
@@ -35,4 +35,13 @@ public class SortUtilsRandomGenerator {
         return random.nextDouble();
     }
 
+    /**
+     * Function to generate int value.
+     *
+     * @return int value [0, n)
+     */
+    public static int generateInt(int n) {
+        return random.nextInt(n);
+    }
+
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java
index 280b512e..2202872f 100644
--- a/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java
@@ -106,7 +106,7 @@ class InsertionSortTest {
     }
 
     private void testWithRandomArray(Function<Double[], Double[]> sortAlgorithm) {
-        int randomSize = (int) (SortUtilsRandomGenerator.generateDouble() * 10_000);
+        int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
         Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
         Double[] sorted = sortAlgorithm.apply(array);
         assertTrue(SortUtils.isSorted(sorted));
diff --git a/src/test/java/com/thealgorithms/sorts/MergeSortTest.java b/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
index 8958fb30..2f127705 100644
--- a/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
@@ -1,13 +1,19 @@
 package com.thealgorithms.sorts;
 
-import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class MergeSortTest {
 
-    private static MergeSort mergeSort= new MergeSort();
+    private MergeSort mergeSort;
+
+    @BeforeEach
+    void setUp() {
+        mergeSort = new MergeSort();
+    }
 
     @Test
     void shouldAcceptWhenEmptyArrayIsPassed() {
@@ -79,5 +85,11 @@ public class MergeSortTest {
         assertArrayEquals(expected, sorted);
     }
 
-
+    @Test
+    void shouldAcceptWhenRandomArrayIsPassed() {
+        int randomSize = SortUtilsRandomGenerator.generateInt(10_000);
+        Double[] array = SortUtilsRandomGenerator.generateArray(randomSize);
+        Double[] sorted = mergeSort.sort(array);
+        assertTrue(SortUtils.isSorted(sorted));
+    }
 }