11package Sorts ;
22
3- import static Sorts .SortUtils .print ;
4-
53/**
6- * This method implements the Generic Merge Sort
4+ * Generic merge sort algorithm.
75 *
8- * @author Varun Upadhyay (https://github.com/varunu28)
9- * @author Podshivalov Nikita (https://github.com/nikitap492)
106 * @see SortAlgorithm
117 */
128class MergeSort implements SortAlgorithm {
139
1410 /**
15- * This method implements the Generic Merge Sort
11+ * Generic merge sort algorithm implements.
1612 *
17- * @param unsorted the array which should be sorted
18- * @param <T> Comparable class
19- * @return sorted array
13+ * @param unsorted the array which should be sorted.
14+ * @param <T> Comparable class.
15+ * @return sorted array.
2016 */
2117 @ Override
2218 public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
@@ -25,29 +21,30 @@ public <T extends Comparable<T>> T[] sort(T[] unsorted) {
2521 }
2622
2723 /**
28- * @param arr The array to be sorted
29- * @param left The first index of the array
30- * @param right The last index of the array Recursively sorts the array in increasing order
24+ * @param arr the array to be sorted.
25+ * @param left the first index of the array.
26+ * @param right the last index of the array.
3127 */
3228 private static <T extends Comparable <T >> void doSort (T [] arr , int left , int right ) {
3329 if (left < right ) {
34- int mid = left + ( right - left ) / 2 ;
30+ int mid = ( left + right ) >>> 1 ;
3531 doSort (arr , left , mid );
3632 doSort (arr , mid + 1 , right );
3733 merge (arr , left , mid , right );
3834 }
3935 }
4036
4137 /**
42- * This method implements the merge step of the merge sort
38+ * Merges two parts of an array.
4339 *
44- * @param arr The array to be sorted
45- * @param left The first index of the array
46- * @param mid The middle index of the array
47- * @param right The last index of the array merges two parts of an array in increasing order
40+ * @param arr the array to be merged.
41+ * @param left the first index of the array.
42+ * @param mid the middle index of the array.
43+ * @param right the last index of the array merges two parts of an array in increasing order.
4844 */
4945 private static <T extends Comparable <T >> void merge (T [] arr , int left , int mid , int right ) {
5046 int length = right - left + 1 ;
47+ @ SuppressWarnings ("unchecked" )
5148 T [] temp = (T []) new Comparable [length ];
5249 int i = left ;
5350 int j = mid + 1 ;
@@ -72,21 +69,20 @@ private static <T extends Comparable<T>> void merge(T[] arr, int left, int mid,
7269 System .arraycopy (temp , 0 , arr , left , length );
7370 }
7471
75- // Driver program
72+ /** Driver code */
7673 public static void main (String [] args ) {
74+ MergeSort mergeSort = new MergeSort ();
7775
78- // Integer Input
7976 Integer [] arr = {4 , 23 , 6 , 78 , 1 , 54 , 231 , 9 , 12 };
80- MergeSort mergeSort = new MergeSort ();
8177 mergeSort .sort (arr );
78+ for (int i = 0 ; i < arr .length - 1 ; ++i ) {
79+ assert arr [i ] <= arr [i + 1 ];
80+ }
8281
83- // Output => 1 4 6 9 12 23 54 78 231
84- print (arr );
85-
86- // String Inpu
8782 String [] stringArray = {"c" , "a" , "e" , "b" , "d" };
8883 mergeSort .sort (stringArray );
89- // Output => a b c d e
90- print (stringArray );
84+ for (int i = 0 ; i < stringArray .length - 1 ; ++i ) {
85+ assert arr [i ].compareTo (arr [i + 1 ]) <= 0 ;
86+ }
9187 }
9288}
0 commit comments