|
1 |
| -import java.util.Scanner; |
2 |
| - |
3 | 1 | /**
|
4 |
| - * This class implements MergeSort |
5 |
| - * @author Unknown |
| 2 | + * |
| 3 | + * @author Varun Upadhyay (https://github.com/varunu28) |
6 | 4 | *
|
7 | 5 | */
|
8 |
| -public class MergeSort { |
9 |
| - /** Array for mergeSort*/ |
10 |
| - private int[] array; |
11 |
| - /** Temp Merge Array*/ |
12 |
| - private int[] tempMergArr; |
13 |
| - /** Length of the array*/ |
14 |
| - private int length; |
15 | 6 |
|
16 |
| - /** |
17 |
| - * Sorts inputArr with merge sort algorithm |
18 |
| - * |
19 |
| - * @param inputArr Array to be sorted |
20 |
| - */ |
21 |
| - public final void sort(int inputArr[]) { |
22 |
| - this.array = inputArr; |
23 |
| - this.length = inputArr.length; |
24 |
| - this.tempMergArr = new int[this.length]; |
25 |
| - this.mergeSort(0, this.length - 1); |
26 |
| - } |
| 7 | +class MergeSort { |
27 | 8 |
|
28 | 9 | /**
|
29 |
| - * Partitions Array into recursively smaller pieces |
| 10 | + * This method implements the Generic Merge Sort |
30 | 11 | *
|
31 |
| - * @param lowerIndex lower bound to include in the first partition |
32 |
| - * @param higherIndex upper bound to include in the third partition |
33 |
| - */ |
34 |
| - private void mergeSort(int lowerIndex, int higherIndex) { |
35 |
| - if (lowerIndex < higherIndex) { |
36 |
| - int middle = lowerIndex + (higherIndex - lowerIndex) / 2; |
37 |
| - // Below step sorts the left side of the array |
38 |
| - this.mergeSort(lowerIndex, middle); |
39 |
| - // Below step sorts the right side of the array |
40 |
| - this.mergeSort(middle + 1, higherIndex); |
41 |
| - // Now merge both sides |
42 |
| - this.mergeParts(lowerIndex, middle, higherIndex); |
| 12 | + * @param arr The array to be sorted |
| 13 | + * @param temp The copy of the actual array |
| 14 | + * @param left The first index of the array |
| 15 | + * @param right The last index of the array |
| 16 | + * Recursively sorts the array in increasing order |
| 17 | + **/ |
| 18 | + |
| 19 | + public static <T extends Comparable<T>> void MS(T[] arr, T[] temp, int left, int right) { |
| 20 | + if (left < right) { |
| 21 | + int mid = left + (right - left) / 2; |
| 22 | + MS(arr, temp, left, mid); |
| 23 | + MS(arr, temp,mid + 1, right); |
| 24 | + merge(arr, temp, left, mid, right); |
43 | 25 | }
|
| 26 | + |
44 | 27 | }
|
45 | 28 |
|
46 | 29 | /**
|
47 |
| - * Merges partitions |
| 30 | + * This method implements the merge step of the merge sort |
48 | 31 | *
|
49 |
| - * @param lowerIndex The lower index |
50 |
| - * @param middle The middle index |
51 |
| - * @param higherIndex The higher index |
52 |
| - */ |
53 |
| - private void mergeParts(int lowerIndex, int middle, int higherIndex) { |
54 |
| - for (int i = lowerIndex; i <= higherIndex; i++) { |
55 |
| - this.tempMergArr[i] = this.array[i]; |
| 32 | + * @param arr The array to be sorted |
| 33 | + * @param temp The copy of the actual array |
| 34 | + * @param left The first index of the array |
| 35 | + * @param mid The middle index of the array |
| 36 | + * @param right The last index of the array |
| 37 | + * merges two parts of an array in increasing order |
| 38 | + **/ |
| 39 | + |
| 40 | + public static <T extends Comparable<T>> void merge(T[] arr, T[] temp, int left, int mid, int right) { |
| 41 | + for (int i=left;i<=right;i++) { |
| 42 | + temp[i] = arr[i]; |
56 | 43 | }
|
57 |
| - int i = lowerIndex; |
58 |
| - int j = middle + 1; |
59 |
| - int k = lowerIndex; |
60 |
| - while (i <= middle && j <= higherIndex) { |
61 |
| - if (this.tempMergArr[i] <= this.tempMergArr[j]) { |
62 |
| - this.array[k] = this.tempMergArr[i]; |
| 44 | + |
| 45 | + int i= left; |
| 46 | + int j = mid + 1; |
| 47 | + int k = left; |
| 48 | + |
| 49 | + while (i<=mid && j<=right) { |
| 50 | + if (temp[i].compareTo(temp[j]) <= 0) { |
| 51 | + arr[k] = temp[i]; |
63 | 52 | i++;
|
64 |
| - } else { |
65 |
| - this.array[k] = this.tempMergArr[j]; |
| 53 | + } |
| 54 | + else { |
| 55 | + arr[k] = temp[j]; |
66 | 56 | j++;
|
67 | 57 | }
|
68 | 58 | k++;
|
69 | 59 | }
|
70 |
| - while (i <= middle) { |
71 |
| - this.array[k] = this.tempMergArr[i]; |
72 |
| - k++; |
| 60 | + |
| 61 | + while (i <= mid) { |
| 62 | + arr[k] = temp[i]; |
73 | 63 | i++;
|
| 64 | + k++; |
74 | 65 | }
|
75 |
| - |
76 | 66 | }
|
77 | 67 |
|
78 |
| - /** |
79 |
| - * Gets input to sort |
80 |
| - * |
81 |
| - * @return unsorted array of integers to sort |
82 |
| - */ |
83 |
| - public static int[] getInput() { |
84 |
| - final int numElements = 6; |
85 |
| - int[] unsorted = new int[numElements]; |
86 |
| - Scanner input = new Scanner(System.in); |
87 |
| - System.out.println("Enter any 6 Numbers for Unsorted Array : "); |
88 |
| - for (int i = 0; i < numElements; i++) { |
89 |
| - unsorted[i] = input.nextInt(); |
| 68 | + // Driver program |
| 69 | + public static void main(String[] args) { |
| 70 | + |
| 71 | + // Integer Input |
| 72 | + int[] arr = {4,23,6,78,1,54,231,9,12}; |
| 73 | + Integer[] array = new Integer[arr.length]; |
| 74 | + for (int i=0;i<array.length;i++) { |
| 75 | + array[i] = arr[i]; |
90 | 76 | }
|
91 |
| - input.close(); |
92 |
| - return unsorted; |
93 |
| - } |
94 | 77 |
|
95 |
| - /** |
96 |
| - * Main Method |
97 |
| - * |
98 |
| - * @param args Command line arguments |
99 |
| - */ |
100 |
| - public static void main(String args[]) { |
101 |
| - int[] inputArr = getInput(); |
102 |
| - MergeSort mergeSort = new MergeSort(); |
103 |
| - mergeSort.sort(inputArr); |
104 |
| - for (int i : inputArr) { |
105 |
| - System.out.print(i); |
106 |
| - System.out.print(" "); |
| 78 | + // Copy of actual array |
| 79 | + Integer[] temp = new Integer[arr.length]; |
| 80 | + |
| 81 | + MS(array, temp, 0, arr.length-1); |
| 82 | + |
| 83 | + // Output => 1 4 6 9 12 23 54 78 231 |
| 84 | + for (int i=0;i<arr.length;i++) { |
| 85 | + System.out.print(array[i] + " "); |
| 86 | + } |
| 87 | + System.out.println(); |
| 88 | + |
| 89 | + // String Input |
| 90 | + String[] array1 = {"c", "a", "e", "b","d"}; |
| 91 | + String[] temp1 = new String[array1.length]; |
| 92 | + MS(array1, temp1, 0, array1.length-1); |
| 93 | + |
| 94 | + //Output => a b c d e |
| 95 | + for(int i=0; i<array1.length; i++) |
| 96 | + { |
| 97 | + System.out.print(array1[i]+"\t"); |
107 | 98 | }
|
108 | 99 | }
|
109 | 100 | }
|
0 commit comments