-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.cpp
More file actions
65 lines (64 loc) · 1.04 KB
/
merge_sort.cpp
File metadata and controls
65 lines (64 loc) · 1.04 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
#include<stdio.h>
int count = 0;
void merge(int A[],int l,int m,int r){
int B[r+1];
int k=l;
int x=l;
int y=m;
count+=4;
while(x<m&&y<=r){
if(A[x]<A[y]){
B[k] = A[x];
x++;
count+=3;
}
else{
B[k] = A[y];
y++;
count+=3;
}
k++;
count+=2;
}
while(x<m){
B[k] = A[x];
k++;
x++;
count+=5;
}
while(y<=r){
B[k] = A[y];
k++;
y++;
count+=5;
}
for(k=l;k<r+1;k++){
A[k] = B[k];
count+=5;
}
}
void merge_sort(int A[],int l, int r){
if(l<r){
int m = (l+r)/2;
merge_sort(A,l,m);
merge_sort(A,m+1,r);
merge(A,l,m+1,r);
}
count+=6;
}
void display(int A[],int n){
int i;
for(i=0;i<n;i++){
printf("%d\t",A[i]);
}
}
int main(){
int A[]= {2,7,8,9,1,4,3,5,6,22,34,1,75,45,31,17,23,42,11,60};
int n = sizeof(A)/sizeof(int);
printf("The unsorted array is: \n");
display(A,n);
merge_sort(A,0,n-1);
printf("\n\nThe sorted array after Merge Sort: \n");
display(A,n);
printf("\n\nNumber of steps = %d with size of array = %d\n",count,n);
}