-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.java
76 lines (64 loc) · 2.1 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
import java.util.ArrayList;
public class MergeSort {
public void merge(ArrayList<Student> students, ArrayList<Student> left, ArrayList<Student> right) {
ArrayList<Student> tempArray = new ArrayList<>();
int currentIndex = 0; // pointer index for sorted array
int leftIndex = 0; // index for left array
int rightIndex = 0; // index for right array
// while left and right arrays still have values
// do the sorting
while (leftIndex < left.size() && rightIndex < right.size()) {
// if element at left is less than element at right
if (left.get(leftIndex).getRollNo() < right.get(rightIndex).getRollNo()) {
// insert element at new index
students.set(currentIndex, left.get(leftIndex));
leftIndex++;
// else if right is greater than left
} else {
// insert element at new index
students.set(currentIndex, right.get(rightIndex));
rightIndex++;
}
// increment new index for next iteration
// after smallest number has been appended
currentIndex++;
}
int tempIndex = 0; // pointer/placeholder index
// for elements remaining in left or right index
// copy it/those to placeholder array
if (leftIndex < left.size()) {
tempArray = left;
tempIndex = leftIndex;
} else {
tempArray = right;
tempIndex = rightIndex;
}
// append placeholder array to student array
for (int index = tempIndex; index < tempArray.size(); index++) {
students.set(currentIndex, tempArray.get(index));
currentIndex++;
}
}
public void mergeSort(ArrayList<Student> students) {
int middle;
ArrayList<Student> left = new ArrayList<>();
ArrayList<Student> right = new ArrayList<>();
if (students.size() > 1) {
middle = students.size() / 2;
// get left array
for (int i = 0; i < middle; i++) {
left.add(students.get(i));
}
// get right array
for (int j = middle; j < students.size(); j++) {
right.add(students.get(j));
}
// recursion to divide array into left and right
// and sort each
mergeSort(left);
mergeSort(right);
// merge two arrays together
merge(students, left, right);
}
}
}