-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
47 lines (47 loc) · 990 Bytes
/
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
import java.util.*;
public class MergeSort {
private static void merge(int[] arr,int low1,int high1,int low2,int high2) {
int n=high2-low1+1;
int[] temp=new int[n];
int i=low1,j=low2,k=0;
while(i<=high1 && j<=high2) {
if(arr[i]<=arr[j]) {
temp[k++]=arr[i++];
}
else {
temp[k++]=arr[j++];
}
}
while(i<=high1) {
temp[k++]=arr[i++];
}
while(j<=high2) {
temp[k++]=arr[j++];
}
for(int l=low1;l<=high2;l++) {
arr[l]=temp[l-low1];
}
}
public static void sort(int[] arr,int low,int high) {
int mid;
if(low<high) {
mid=(low+high)/2;
sort(arr,low,mid);
sort(arr,mid+1,high);
merge(arr,low,mid,mid+1,high);
}
}
public static void main(String[] args) {
int[] arr;
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
sort(arr,0,n-1);
System.out.println(Arrays.toString(arr));
sc.close();
}
}