-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.java
66 lines (52 loc) · 1.47 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
package com.keshav.ctci.sorting;
import static com.keshav.ctci.util.ArrayUtils.swap;
import java.util.Arrays;
public class MergeSort {
public static void sort(int arr[]) {
if (arr.length < 2) {
return;
}
sort(arr, 0, arr.length - 1);
}
private static void sort(int arr[], int low, int high) {
if (low >= high) return;
int mid = (low+high)/2;
sort(arr, low, mid);
sort(arr, mid+1, high);
merge(arr, low, mid, high);
}
// merge two sorted sides of an array.
// left pointer and right pointer.
private static void merge(int arr[], int low, int mid, int high) {
int b[] = new int[high - low + 1];
int lp = low;
int rp = mid+1;
int p=0;
while (lp <= mid && rp <= high) {
if (arr[lp] < arr[rp]) {
b[p++] = arr[lp++];
}
else {
b[p++] = arr[rp++];
}
}
while (lp <= mid) {
b[p++] = arr[lp++];
}
while (rp <= high) {
b[p++] = arr[rp++];
}
// add elements to a
for (p=low; p<=high; p++) {
arr[p] = b[p - low];
}
}
public static void main(String args[]) {
int[] a = {5,4,1,3,2};
sort(a);
System.out.println(Arrays.toString(a));
int[] b = {5,4,1,3,2, 11, 7, 0};
sort(b);
System.out.println(Arrays.toString(b));
}
}