-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.cpp
More file actions
48 lines (45 loc) · 915 Bytes
/
Copy pathmerge.cpp
File metadata and controls
48 lines (45 loc) · 915 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
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
void merge(int arr[],int l,int m,int u) {
int i=l,j=m+1,k=0;
int temp[100]={0};
while(i<=m && j<=u) {
if(arr[i]>arr[j]) {
temp[k]=arr[j];
j++;
k++;
}
else {
temp[k]=arr[i];
i++;
k++;
}
}
while(i<=m) {
temp[k]=arr[i];
i++;
k++;
}
while(j<=u) {
temp[k]=arr[j];
j++;
k++;
}
for(int i=l;i<=u;i++)
arr[i]=temp[i-l];
}
void mergesort(int arr[],int l,int u) {
if(l<u) {
int m=int((l+u)/2);
mergesort(arr,l,m);
mergesort(arr,m+1,u);
merge(arr,l,m,u);
}
}
int main() {
int arr[]={9,8,7,6,5,4,3,2,1,0};
mergesort(arr,0,9);
for(int i=0;i<10;i++)
cout<<arr[i]<<" ";
return 0;
}