forked from haitong/Lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
executable file
·61 lines (52 loc) · 1.75 KB
/
MergeSort.java
File metadata and controls
executable file
·61 lines (52 loc) · 1.75 KB
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
/*
Best: O(n log n)
Average: O(n log n)
Worst: O(n log n)
*/
import java.util.*;
public class MergeSort {
public static void mergesort(int[] array) {
mergesort(array, 0, array.length - 1);
}
public static void mergesort(int[] array, int low, int high) {
if (low < high) {
int middle = (low + high) / 2;
mergesort(array, low, middle); // Sort left half
mergesort(array, middle+1, high); // Sort right half
merge(array, low, middle, high); // Merge them
}
}
public static void merge(int[] array, int low, int middle, int high) {
// Copy both halves into a helper array, remember: cannot simply do "helper = array"
int[] helper = new int[array.length];
for (int i = low; i <= high; i++) {
helper[i] = array[i];
}
int helperLeft = low;
int helperRight = middle + 1;
int current = low;
// Iterate through helper array. Compare the left and right half, copying back the smaller element from the two halves into the original array
while (helperLeft <= middle && helperRight <= high) {
if (helper[helperLeft] <= helper[helperRight]) {
array[current] = helper[helperLeft];
helperLeft++;
} else { // If right element is smaller than left element
array[current] = helper[helperRight];
helperRight++;
}
current++;
}
// Copy the rest of the left side of the array into the target array
int remaining = middle - helperLeft;
for (int i = 0; i <= remaining; i++) {
array[current + i] = helper[helperLeft + i];
}
}
public static void main(String[] args) {
int[] array = {5,3,6,1,7,2,4,9,8,0};
System.out.println(Arrays.toString(array));
mergesort(array);
System.out.println(Arrays.toString(array));
}
}
// https://youtu.be/EeQ8pwjQxTM