|
| 1 | +/** |
| 2 | + * Algorithms-in-Java |
| 3 | + * MergeSort.java |
| 4 | + */ |
| 5 | +package com.deepak.algorithms.Sorting; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class for MergeSort implementation |
| 11 | + * @author Deepak |
| 12 | + */ |
| 13 | +public class MergeSort { |
| 14 | + |
| 15 | + /** |
| 16 | + * Main method to start the flow of program |
| 17 | + * @param args |
| 18 | + */ |
| 19 | + public static void main(String[] args) { |
| 20 | + int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10}; |
| 21 | + System.out.println("******************* MERGE - SORT *******************"); |
| 22 | + int[] sortedValues = performMergeSort(valuesToBeSorted, 0, valuesToBeSorted.length - 1); |
| 23 | + Arrays.stream(sortedValues).forEach(System.out::println); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Merge Sort implementation |
| 28 | + * |
| 29 | + * <p>Time Complexity</p> |
| 30 | + * Whenever there are inner loops associated, complexity is n^2. In this case, |
| 31 | + * Best - O(n log(n)) |
| 32 | + * Average - O(n log(n)) |
| 33 | + * Worst - O(n log(n)) |
| 34 | + * |
| 35 | + * @param listOfValues - List of values passed in the request |
| 36 | + */ |
| 37 | + public static int[] performMergeSort(int[] iListOfValues, int iLowestPosition, int iHighestPosition) { |
| 38 | + if (iLowestPosition < iHighestPosition) { |
| 39 | + int middlePosition = iLowestPosition + (iHighestPosition - iLowestPosition) / 2; |
| 40 | + performMergeSort(iListOfValues, iLowestPosition, middlePosition); |
| 41 | + performMergeSort(iListOfValues, middlePosition + 1, iHighestPosition); |
| 42 | + merge(iListOfValues, iLowestPosition, middlePosition, iHighestPosition); |
| 43 | + } |
| 44 | + return iListOfValues; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Method to merge two items from the list |
| 49 | + * @param iListOfValues |
| 50 | + * @param iLowestPosition |
| 51 | + * @param middlePosition |
| 52 | + * @param iHighestPosition |
| 53 | + * @return {@link int[]} |
| 54 | + */ |
| 55 | + private static int[] merge(int[] iListOfValues, int iLowestPosition, int middlePosition, int iHighestPosition) { |
| 56 | + int[] copyOfArrays = new int[iListOfValues.length]; |
| 57 | + for (int i = 0; i < iListOfValues.length; i++ ) { |
| 58 | + copyOfArrays[i] = iListOfValues[i]; |
| 59 | + } |
| 60 | + |
| 61 | + int i = iLowestPosition; |
| 62 | + int j = middlePosition + 1; |
| 63 | + int k = iLowestPosition; |
| 64 | + |
| 65 | + while (i <= middlePosition && j <= iHighestPosition) { |
| 66 | + if (copyOfArrays[i] <= copyOfArrays[j]) { |
| 67 | + iListOfValues[k] = copyOfArrays[i]; |
| 68 | + i++; |
| 69 | + } else { |
| 70 | + iListOfValues[k] = copyOfArrays[j]; |
| 71 | + j++; |
| 72 | + } |
| 73 | + k++; |
| 74 | + } |
| 75 | + while (i <= middlePosition) { |
| 76 | + iListOfValues[k] = copyOfArrays[i]; |
| 77 | + k++; |
| 78 | + i++; |
| 79 | + } |
| 80 | + return iListOfValues; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments