-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathMergeSort.java
64 lines (57 loc) · 1.95 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
import java.util.Arrays;
class MergeSort {
//Main function. Instanciates the unsorted array and calls mergesort.
public static void main(String args[]) {
int intArray[] = {9, 5, 4, 6, 1, 2, 3, 1, 2, 3};
System.out.println("The unsorted array is:");
System.out.println(Arrays.toString(intArray));
mergesort(intArray);
System.out.println("The sorted array is:");
System.out.println(Arrays.toString(intArray));
}
//Splits array by half, creating two new arrays.
//Calls mergesort recursively on both of them.
//(they are expected to be returned sorted)
//then unifies them on the initial array calling merge.
static void mergesort(int intArray[]) {
int size = intArray.length;
if (size < 2) return;
int mid = size / 2;
int left[] = new int[mid];
int right[] = new int[size - mid];
for (int i = 0; i < mid; i++) left[i] = intArray[i];
for (int i = mid; i < size; i++) right[i - mid] = intArray[i];
mergesort(left);
mergesort(right);
merge(intArray, left, right);
}
//Unifies left and right on array.
//Both of them are expected to be already sorted.
//Goes through every index of left and right
//comparing values and saves the lesser one on array.
public static void merge(int array[], int left[], int right[]) {
int sizeLeft = left.length;
int sizeRight = right.length;
int i = 0, j = 0, k = 0;
while (i < sizeLeft && j < sizeRight) {
if (left[i] <= right[j]) {
array[k] = left[i];
i++;
} else {
array[k] = right[i];
j++;
}
k++;
}
while (i < sizeLeft) {
array[k] = left[i];
i++;
k++;
}
while (j < sizeRight) {
array[k] = right[j];
j++;
k++;
}
}
}