-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmergeSort.java
50 lines (45 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
public class mergeSort {
public static int[] merge2sortedArray(int[] arr1 , int[] arr2){ // returns a combined sorted array
int n = arr1.length;
int m = arr2.length;
int[] sum = new int[n + m];
if(n == 0 || m == 0 ){
if(n == 0)
return arr1;
else
return arr2;
}
int i = 0 , j = 0, k = 0;
while(i < n && j < m){
if(arr1[i] <= arr2[j]){
sum[k++] = arr1[i++];
}else{
sum[k++] = arr2[j++];
}
}
while(i < n){
sum[k++] = arr1[i++];
}
while(j < m){
sum[k++] = arr2[j++];
}
return sum;
}
public static int[] mergesort(int[] arr ,int si , int ei){
int mid = (si+ei)/2; // finding our mid
if(si == ei){
return new int[]{arr[si]}; // it will insert sorted element one by one in an array
}
int[] left = mergesort(arr, si, mid);// recursive call for left half.
int[] right = mergesort(arr, mid+1, ei); // recursive call for right half.
return merge2sortedArray(left, right);// combining the left and right sorted array
}
public static void main(String[] args){
int[] arr = {-12,2,34,-29,34,13,-1,0,36,25};
int n = arr.length;
int[] ans = mergesort(arr,0,n-1);
for(int i = 0 ; i < ans.length; i++){
System.out.print(ans[i]+" ");
}
}
}