-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.cpp
More file actions
77 lines (65 loc) · 1.32 KB
/
merge_sort.cpp
File metadata and controls
77 lines (65 loc) · 1.32 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
/*
Best-case time complexity O(nlogn)
Average-case time complexity O(nlogn)
worst-case time complexity O(nlogn)
space complexity O(n)
not stable
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
void merge(int *arr, int *L, int nL,int *R, int nR){
int i=0,j=0,k=0;
while(i<nL && j<nR){
if(L[i]<=R[j]){
arr[k]=L[i];
i++;
}
else{
arr[k]=R[i];
j++;
}
k++;
}
while(i<nL){
arr[k]=L[i]; i++; k++;
}
while(j<nR){
arr[k]=R[j]; j++; k++;
}
}
void merge_sort(int *arr,int n){
if(n<2) return;
int mid, *L, *R;
mid=n/2;
L=(int*)malloc(mid*sizeof(int));
R=(int*)malloc((n-mid)*sizeof(int));
for(int i=0;i<mid;++i){
L[i]=arr[i];
}
for(int i=mid;i<n;++i){
R[i-mid]=arr[i];
}
merge_sort(L,mid);
merge_sort(R,n-mid);
merge(arr,L,mid,R,n-mid);
free(L);
free(R);
}
int main(){
cout<<"enter the size of array?"<<endl;
int n,i,j,k;
cin>>n;
cout<<"enter the elements of the array......."<<endl;
int arr[n];
for(i=0;i<n;++i){
cin>>arr[i];
}
merge_sort(arr,n);
cout<<"sorted array: ";
for(i=0;i<n;++i){
cout<<arr[i]<<" ";
}cout<<endl;
return 0;
}