forked from namishkhanna/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
53 lines (45 loc) · 870 Bytes
/
MergeSort.cpp
File metadata and controls
53 lines (45 loc) · 870 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
49
50
51
52
53
#include <iostream>
using namespace std;
void display(int* arr,int n) {
for(int i=0; i<n; ++i)
cout<<arr[i]<<" ";
cout<<endl;
}
void merge(int* arr,int start,int end){
int* temp = new int[end-start+1];
int mid = (start+end)/2;
int k = 0, i = start, j = mid+1;
while(i <= mid and j <= end){
if(arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while(i <= mid) {
temp[k++] = arr[i++];
}
while(j <= end) {
temp[k++] = arr[j++];
}
for(int i=start; i<=end; ++i) {
arr[i] = temp[i-start];
}
}
void mergesort(int* arr,int start,int end) {
if(start == end)
return;
int mid = (start+end)/2;
mergesort(arr,start,mid);
mergesort(arr,mid+1,end);
merge(arr,start,end);
}
int main() {
int n;
cin>>n;
int* arr = new int[n];
for(int i=0; i<n; ++i)
cin>>arr[i];
mergesort(arr,0,n-1);
display(arr,n);
return 0;
}