-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.java
More file actions
86 lines (65 loc) · 1.89 KB
/
merge_sort.java
File metadata and controls
86 lines (65 loc) · 1.89 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
public class merge_sort {
// this is only for printing of our array
public static void print(int a[]){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
}
//this is the recursive function
public static void mergesort(int a[],int si,int ei){
// this is our base condition
if(si>=ei){
return;
}
int mid=(si+ei)/2;
// left divide
mergesort(a,si,mid);
// right divide
mergesort(a,mid+1,ei);
// calling merge function to mergr the left and right array
merge(a,si,mid,ei);
}
// this our merge function ***** this is very important to under stand *****
public static void merge(int a[],int si,int mid,int ei){
int temp[]=new int[ei-si+1];
int i=si;
int j=mid+1;
int t=0;
//chote wale element ko aage lane ke liye
while(i<=mid && j<=ei){
if(a[i]<a[j]){
temp[t]=a[i];
t++;
i++;
}
else{
temp[t]=a[j];
t++;
j++;
}
}
// agr left me koi element bach gya h to use temp array me add krne ke liye
while(i<=mid){
temp[t]=a[i];
t++;
i++;
}
// agr right me koi element bach gya h to use temp array me add krne ke liye
while(j<=ei){
temp[t]=a[j];
t++;
j++;
}
//now copy the temp array to the original array
for(i=si,t=0;t<temp.length;t++,i++){
a[i]=temp[t];
}
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a[]={6,3,9,5,2,8,8,8,6,4,3,21,4,5,6,7,4,3,5};
mergesort(a,0,a.length-1);
print(a);
}
}