-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.java
More file actions
60 lines (51 loc) · 1.31 KB
/
Copy pathmerge.java
File metadata and controls
60 lines (51 loc) · 1.31 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
public class merge {
public static void printArr(int arr[]){
for(int i=0; i<arr.length; i++){
System.out.print(arr[i] + " ");
}
}
public static void mergeSort(int arr[], int si, int ei){
if(si >= ei){ //base case
return;
}
int mid = si + (ei-si)/2;
mergeSort(arr, si,mid); //for left
mergeSort(arr,mid+1, ei); //for right
merge(arr,si,mid,ei);
}
public static void merge(int arr[], int si, int mid, int ei){
//temp arry to store values
int temp[] = new int[ei-si+1];
int i = si;
int j = mid+1;
int k = 0;
while(i<=mid && j<=ei){
if(arr[i]<arr[j]){
temp[k] = arr[i];
i++;
}
else{
temp[k] = arr[j];
j++;
}
k++;
}
//for left
while(i<=mid){
temp[k++] = arr[i++];
}
//for right
while(j<=ei){
temp[k++] = arr[j++];
}
//copy temp to origianl array.
for(k=0, i=si; k<temp.length; k++,i++){
arr[i] = temp[k];
}
}
public static void main(String args[]){
int arr[] = {6,3,9,5,2,8};
mergeSort(arr,0,arr.length-1);
printArr(arr);
}
}