1
1
package Sorts ;
2
2
3
- import static Sorts .SortUtils .print ;
4
-
5
3
/**
6
- * This method implements the Generic Merge Sort
4
+ * Generic merge sort algorithm.
7
5
*
8
- * @author Varun Upadhyay (https://github.com/varunu28)
9
- * @author Podshivalov Nikita (https://github.com/nikitap492)
10
6
* @see SortAlgorithm
11
7
*/
12
8
class MergeSort implements SortAlgorithm {
13
9
14
10
/**
15
- * This method implements the Generic Merge Sort
11
+ * Generic merge sort algorithm implements.
16
12
*
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.
20
16
*/
21
17
@ Override
22
18
public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
@@ -25,29 +21,30 @@ public <T extends Comparable<T>> T[] sort(T[] unsorted) {
25
21
}
26
22
27
23
/**
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.
31
27
*/
32
28
private static <T extends Comparable <T >> void doSort (T [] arr , int left , int right ) {
33
29
if (left < right ) {
34
- int mid = left + ( right - left ) / 2 ;
30
+ int mid = ( left + right ) >>> 1 ;
35
31
doSort (arr , left , mid );
36
32
doSort (arr , mid + 1 , right );
37
33
merge (arr , left , mid , right );
38
34
}
39
35
}
40
36
41
37
/**
42
- * This method implements the merge step of the merge sort
38
+ * Merges two parts of an array.
43
39
*
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.
48
44
*/
49
45
private static <T extends Comparable <T >> void merge (T [] arr , int left , int mid , int right ) {
50
46
int length = right - left + 1 ;
47
+ @ SuppressWarnings ("unchecked" )
51
48
T [] temp = (T []) new Comparable [length ];
52
49
int i = left ;
53
50
int j = mid + 1 ;
@@ -72,21 +69,20 @@ private static <T extends Comparable<T>> void merge(T[] arr, int left, int mid,
72
69
System .arraycopy (temp , 0 , arr , left , length );
73
70
}
74
71
75
- // Driver program
72
+ /** Driver code */
76
73
public static void main (String [] args ) {
74
+ MergeSort mergeSort = new MergeSort ();
77
75
78
- // Integer Input
79
76
Integer [] arr = {4 , 23 , 6 , 78 , 1 , 54 , 231 , 9 , 12 };
80
- MergeSort mergeSort = new MergeSort ();
81
77
mergeSort .sort (arr );
78
+ for (int i = 0 ; i < arr .length - 1 ; ++i ) {
79
+ assert arr [i ] <= arr [i + 1 ];
80
+ }
82
81
83
- // Output => 1 4 6 9 12 23 54 78 231
84
- print (arr );
85
-
86
- // String Inpu
87
82
String [] stringArray = {"c" , "a" , "e" , "b" , "d" };
88
83
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
+ }
91
87
}
92
88
}
0 commit comments