-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.java
82 lines (70 loc) · 2.43 KB
/
MergeSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package dsa_dp.algorithms.sorting;
import java.util.ArrayList;
import java.util.Collections;
public class MergeSort<T extends Comparable<T>> {
/**
* Sorts the given array using the 'MergeSort' algorithm
*
* @param arr the input arrayList that needs to be sorted
*/
public void sort(ArrayList<T> arr) {
int size = arr.size();
if (size > 1) {
int halfSize = size / 2;
ArrayList<T> leftHalf = new ArrayList<>(Collections.nCopies(halfSize, null));
ArrayList<T> rightHalf = new ArrayList<>(Collections.nCopies(size - halfSize, null));
populateHalves(arr, leftHalf, rightHalf, size, halfSize);
sort(leftHalf);
sort(rightHalf);
merge(arr, leftHalf, rightHalf);
}
}
/**
* Populates the two given halves array with the
* correct values of the input array
*
* @param inputArr reference to the input array
* @param leftHalf left half of the input array
* @param rightHalf right half of the input array
* @param size size of the input array
* @param halfSize half of the input array size
*/
private void populateHalves(
ArrayList<T> inputArr,
ArrayList<T> leftHalf,
ArrayList<T> rightHalf,
int size,
int halfSize
) {
// Populating the left half
for (int i = 0; i < halfSize; i++) {
leftHalf.set(i, inputArr.get(i));
}
// Populating the right half
for (int i = halfSize; i < size; i++) {
rightHalf.set(i - halfSize, inputArr.get(i));
}
}
/**
* Merges the two given arrays into the input array sorting it while merging
*
* @param inputArr reference to the input array
* @param leftHalf left half of the input array
* @param rightHalf right half of the input array
*/
private void merge(ArrayList<T> inputArr, ArrayList<T> leftHalf, ArrayList<T> rightHalf) {
int i = 0, j = 0, k = 0;
int lSize = leftHalf.size();
int rSize = rightHalf.size();
while (k < (lSize + rSize)) {
if (j >= rSize || (i < lSize && leftHalf.get(i).compareTo(rightHalf.get(j)) <= 0)) {
inputArr.set(k, leftHalf.get(i));
i++;
} else {
inputArr.set(k, rightHalf.get(j));
j++;
}
k++;
}
}
}