-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
35 lines (30 loc) · 780 Bytes
/
MergeSort.java
File metadata and controls
35 lines (30 loc) · 780 Bytes
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
public class MergeSort {
public static void sort(int a[],int left,int right){
int k = (right - left)/2;
if(left != right){
sort(a, left,k);
sort(a, k+1,right);
}else{return;}
int n = left ,q = k+1;
int b[] = new int[right -left +1];
for(int i = 0;i<right - left + 1;i++ ){
if (n==k+1){
b[i]= a[q];
q++;
}
if(a[n]<a[q]){
b[i]= a[n];
n++;
}else if(a[n]>a[q]){
b[i]= a[q];
q++;
}else{
b[i]= a[n];
n++;
}
}
for(int i = left; i <= right;i++){
a[i]= b[i-left];
}
}
}