-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge Sort
More file actions
54 lines (51 loc) · 1.02 KB
/
Copy pathMerge Sort
File metadata and controls
54 lines (51 loc) · 1.02 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
#include <stdio.h>
void display(int arr[],int n){
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
printf("\n");
}
void merge(int arr[],int low,int mid,int high){
int i=low,h=low;
int j=mid+1;
int k,temp[100];
while(h<=mid && j<=high){
if(arr[h]<=arr[j]){
temp[i]=arr[h];
h++;
}else{
temp[i]=arr[j];
j++;
}
i++;
}
if(h<=mid){
for(k=h;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}else{
for(k=j;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
void mergeSort(int arr[],int low,int high){
if(low<high){
int mid=low+(high-low)/2;
mergeSort(arr,low,mid);
mergeSort(arr,mid+1,high);
merge(arr,low,mid,high);
}
}
int main(){
int arr[10]={21,45,37,15,14,35,90,46,67,30};
display(arr,10);
mergeSort(arr,0,10-1);
display(arr,10);
return 0;
}