forked from dharmanshu1921/Daa-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sort.cpp
More file actions
62 lines (62 loc) · 1.13 KB
/
Merge_Sort.cpp
File metadata and controls
62 lines (62 loc) · 1.13 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
#include <iostream>
using namespace std;
void merging(int*a,int start,int mid,int end){
int *temp= new int [end -start+1];
int j,k=0,i;
for( i=start,j=mid+1;i<=mid && j<=end;){
if(a[i]>a[j]){
temp[k]=a[j];
j++;
k++;
}
else{
temp[k]=a[i];
i++;
k++;
}
}
while(i<=mid){
temp[k]=a[i];
i++;
k++;
}
while(j<=end){
temp[k]=a[j];
j++;
k++;
}
for(int i=start;i<=end;i++){
cout<<a[i]<<" ";
}
cout<<endl;
int index=start;
for(int i=0;i<k;i++){
a[index++]=temp[i];
}
}
void merge(int *a,int start,int end){
if(start>=end){
return;
}
int mid=(start+end)/2;
merge(a,start,mid);
merge(a,mid+1,end);
merging(a,start,mid,end);
}
void mergesort(int *a, int n){
merge(a,0,n);
}
int main(){
int n;
cin>>n;
int *a=new int[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
mergesort(a,n-1);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
delete [] a;
}