|
1 | 1 | package src.algorithms.sorting.mergeSort.iterative;
|
2 | 2 |
|
| 3 | +/** Here, we are implementing MergeSort where we sort the array in increasing (or more precisely, non-decreasing) |
| 4 | + * order iteratively. |
| 5 | + * |
| 6 | + * Brief Description: |
| 7 | + * The iterative implementation of MergeSort takes a bottom-up approach, where the sorting process starts by merging |
| 8 | + * intervals of size 1. Intervals of size 1 are trivially in sorted order. The algorithm then proceeds to merge |
| 9 | + * adjacent sorted intervals, doubling the interval size with each merge step, until the entire array is fully sorted. |
| 10 | + * |
| 11 | + * Implementation Invariant: |
| 12 | + * At each iteration of the merging process, the main array is divided into sub-arrays of a certain interval |
| 13 | + * size (<interval>). Each of these sub-arrays is sorted within itself. The last sub-array may not be of size |
| 14 | + * <interval> but it is still sorted since its size is necessarily less than <interval>. |
| 15 | + * |
| 16 | + * Complexity Analysis: |
| 17 | + * Time: |
| 18 | + * - Worst case: O(nlogn) |
| 19 | + * - Average case: O(nlogn) |
| 20 | + * - Best case: O(nlogn) |
| 21 | + * |
| 22 | + * Given two sorted arrays of size p and q, we need O(p + q) time to merge the two arrays into one sorted array, since |
| 23 | + * we have to iterate through every element in both arrays once. |
| 24 | + * |
| 25 | + * At the 1st level of the merge process, our merging subroutine involves n sub-arrays of size 1, taking O(n) time. |
| 26 | + * At the 2nd level of the merge process, our merging subroutine involves (n/2) sub-arrays of size 2, taking O(n) time. |
| 27 | + * At the kth level of the merge process, our merging subroutine involves (n/(2^(k-1))) sub-arrays of size (2^(k-1)), |
| 28 | + * taking O(n) time. |
| 29 | + * |
| 30 | + * Since <interval> doubles at every iteration of the merge process, there are logn such levels. Every level takes |
| 31 | + * O(n) time, hence overall time complexity is n * logn = O(nlogn) |
| 32 | + * |
| 33 | + * Regardless of how sorted the input array is, MergeSort carries out the partitioning and merging process, so the |
| 34 | + * time complexity of MergeSort is O(nlogn) for all cases. |
| 35 | + * |
| 36 | + * Space: |
| 37 | + * - O(n) since we require a temporary array to temporarily store the merged elements in sorted order |
| 38 | + */ |
| 39 | + |
3 | 40 | public class MergeSort {
|
| 41 | + |
| 42 | + /** |
| 43 | + * Sorts the given array in non-decreasing order. |
| 44 | + * |
| 45 | + * @param arr The given array to be sorted. |
| 46 | + */ |
| 47 | + public static void sort(int[] arr) { |
| 48 | + int interval = 1; |
| 49 | + int n = arr.length; |
| 50 | + int[] temp = new int[n]; |
| 51 | + |
| 52 | + while (interval < n) { |
| 53 | + for (int i = 0; i < n - interval; i += 2 * interval) { |
| 54 | + int end = Math.min(i + 2 * interval - 1, n - 1); |
| 55 | + int mid = i + interval - 1; |
| 56 | + merge(arr, i, mid, end, temp); |
| 57 | + } |
| 58 | + interval *= 2; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Merges two sorted sub-arrays within the given array. The two sub-arrays are arr[start, mid] and |
| 64 | + * arr[mid + 1, end]. Upon completion of this function, arr[start, end] will be in sorted order. |
| 65 | + * |
| 66 | + * @param arr The array containing the sub-arrays to be merged. |
| 67 | + * @param start The starting index of the first sub-array to be merged. |
| 68 | + * @param mid The ending index (inclusive) of the first sub-array to be merged. In the iterative implementation, |
| 69 | + * the mid parameter is required in the merge function to determine the splitting point between the |
| 70 | + * sub-arrays to be merged. |
| 71 | + * @param end The ending index (inclusive) of the second sub-array to be merged. |
| 72 | + * @param temp A temporary array used for merging intermediate results. |
| 73 | + */ |
| 74 | + private static void merge(int[] arr, int start, int mid, int end, int[] temp) { |
| 75 | + int i = start; |
| 76 | + int j = mid + 1; |
| 77 | + int pointer = start; |
| 78 | + |
| 79 | + // Merge the two sorted sub-arrays into the temp array |
| 80 | + while (i <= mid && j <= end) { |
| 81 | + if (arr[i] <= arr[j]) { |
| 82 | + //we use <= here to maintain stability of MergeSort. If arr[i] == arr[j], the algorithm prefers the |
| 83 | + //one from the left sub-array (arr[i]). This decision preserves the relative order of equal elements. |
| 84 | + |
| 85 | + //if we change this to arr[i] >= arr[j], we can sort the array in non-increasing order. |
| 86 | + |
| 87 | + temp[pointer] = arr[i]; |
| 88 | + i++; |
| 89 | + } else { |
| 90 | + temp[pointer] = arr[j]; |
| 91 | + j++; |
| 92 | + } |
| 93 | + pointer++; |
| 94 | + } |
| 95 | + |
| 96 | + // Copy any remaining elements from the left sub-array |
| 97 | + while (i <= mid) { |
| 98 | + temp[pointer] = arr[i]; |
| 99 | + i++; |
| 100 | + pointer++; |
| 101 | + } |
| 102 | + |
| 103 | + // Copy any remaining elements from the right sub-array |
| 104 | + while (j <= end) { |
| 105 | + temp[pointer] = arr[j]; |
| 106 | + j++; |
| 107 | + pointer++; |
| 108 | + } |
| 109 | + |
| 110 | + // Copy the merged elements back to the original array |
| 111 | + for (int k = start; k <= end; k++) { |
| 112 | + arr[k] = temp[k]; |
| 113 | + } |
| 114 | + } |
| 115 | + |
4 | 116 | }
|
0 commit comments