-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.java
94 lines (76 loc) · 2.39 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class MergeSort {
// function to do the combine task by mergeProcedure
public static void mergeProcedure(int[] arr, int l, int mid, int r){
int i, j, k;
// n1 - size of left subarray
// n2 - size of right subarray
int n1 = mid - l + 1;
int n2 = r - mid;
// create a left and right subarray
int[] lsubarray = new int[n1];
int[] rsubarray = new int[n2];
// copy the elements into left and right subarray
for(i=0; i<n1; i++){
lsubarray[i] = arr[l+i];
}
for(j=0; j<n2; j++){
rsubarray[j] = arr[mid + 1 + j];
}
// comparison among elements in the left and right subarray
i=0;
j=0;
k=l;
while(i < n1 && j < n2){
if(lsubarray[i] <= rsubarray[j]){
arr[k] = lsubarray[i];
i = i+1;
}
else{
arr[k] = rsubarray[j];
j = j + 1;
}
k = k + 1;
}
// copy the remaining elements from left subarray
while(i < n1){
arr[k] = lsubarray[i];
i = i+1;
k = k+1;
}
// copy the remaining elements from right subarray
while(j < n2){
arr[k] = rsubarray[j];
j = j + 1;
k = k +1;
}
}
// function to do mergesort
public static void mergeSort(int[] arr, int l, int r){
if(l < r){
// 1. Divide the array into various subproblems
int mid = l + (r - l)/2;
// 2. Conquer the subproblems via the recursion
// left subarray - recursive call
mergeSort(arr, l, mid);
// right subarray - recursive call
mergeSort(arr, mid+1, r);
// 3. Combine - mergeProcedure
mergeProcedure(arr, l, mid, r);
}
}
// to display array
public static void printArr(int[] arr, int n){
for(int i=0; i<n; i++){
System.out.println(arr[i]+ " ");
}
}
public static void main(String[] args) {
int[] arr = {50, 20, 40, 90, 88, 11, 13, 19, 27};
int n = arr.length;
System.out.println("Array before sort is: ");
printArr(arr, n);
mergeSort(arr, 0, n-1);
System.out.println("Array after sort is: ");
printArr(arr, n);
}
}