forked from csfx-py/hacktober2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort
More file actions
32 lines (27 loc) · 633 Bytes
/
MergeSort
File metadata and controls
32 lines (27 loc) · 633 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
#include<iostream>
#include<algorithm>
using namespace std;
// sorts sequence s of length n
void mergesort(int s[], int n){
if(n==1) return;
//create u ,v from s
int u[n/2], v[n-n/2]; //local arrays
for(int i=0;i<n/2;i++){
u[i]=s[i]; //copy of first half
}
for(int i=0;i<n-n/2;i++){
v[i]=s[i+n/2]; //copy of second half
}
//sort halves
mergesort(u,n/2);
mergesort(v,n-n/2);
//merge into original array s
merge(u,u+n/2,v,v+(n-n/2),s);
}
int main(){
int a[8]={10,552,36,-55,3,8,-96,4};
mergesort(a,8);
for(int i=0;i<8;i++){
cout<<a[i]<<" ";
}
}