-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesort.java
More file actions
41 lines (41 loc) · 981 Bytes
/
mergesort.java
File metadata and controls
41 lines (41 loc) · 981 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
36
37
38
39
40
41
public class mergesort
{
static void merge(int a[],int aux[],int lo,int mid,int hi)
{
for(int k=lo;k<=hi;k++)
{
aux[k]=a[k];
}
int i=lo,j=mid+1;
for(int k=lo;k<=hi;k++)
{
if(i>mid)
a[k]=aux[j++];
else if(j>hi)
a[k]=aux[i++];
else if(aux[i]<aux[j])
a[k]=aux[i++];
else
a[k]=aux[j++];
}
}
static void sort(int a[],int aux[],int lo,int hi)
{
if(lo>=hi)
return;
int mid=lo+(hi-lo)/2;
sort(a,aux,lo,mid);
sort(a,aux,mid+1,hi);
merge(a,aux,lo,mid,hi);
}
public static void main()
{
int a[]={1,3,2,7,5};
int aux[]=new int[a.length];
sort(a,aux,0,a.length-1);
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}