-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
56 lines (46 loc) · 1.46 KB
/
MergeSort.java
File metadata and controls
56 lines (46 loc) · 1.46 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
package kb.sort;
import kb.sort.api.Sortable;
public class MergeSort implements Sortable {
/**
* Merge Sort. n.log(n) time for worst, average and base case. O(n) space, but
* can be done and with O(1) space. Stable: Yes
*/
@Override
public int[] sort(int[] nums) {
return mergeSort(nums, 0, nums.length - 1);
}
private int[] mergeSort(int[] nums, int lo, int hi) {
int mid = (lo + hi) / 2;
if (lo == hi)
return nums;
nums = mergeSort(nums, lo, mid);
nums = mergeSort(nums, mid + 1, hi);
return merge(nums, lo, mid, hi);
}
/**
* This is the tricky part of the algorithm.
*/
private int[] merge(int[] nums, int lo, int mid, int hi) {
int[] tmp = new int[hi - lo + 1];
int l = lo, r = mid + 1;
int i = 0;
while (l <= mid && r <= hi)
if (nums[l] < nums[r])
tmp[i++] = nums[l++];
else
tmp[i++] = nums[r++];
// copy the rest of the left part
for (int j = l; j <= mid; j++)
tmp[i++] = nums[j];
// copy the rest of the right part
for (int j = r; j <= hi; j++)
tmp[i++] = nums[j];
// copy the merged part to the original array
for (int j = lo; j <= hi; j++)
nums[j] = tmp[j - lo];
return nums;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}