-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
42 lines (38 loc) · 746 Bytes
/
Copy pathMergeSort.cpp
File metadata and controls
42 lines (38 loc) · 746 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
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
void Merge(int *H,int low,int middle,int high)
{
vector<int> cp;
int i,j;
for( i=low,j=middle+1;i<=middle&&j<=high;)
{
if(H[i]>H[j]) cp.push_back(H[j++]);
else cp.push_back(H[i++]);
}
while(i<=middle) cp.push_back(H[i++]);
while(j<=high) cp.push_back(H[j++]);
copy(cp.begin(),cp.end(),H+low);
}
void MSort(int *H,int low,int high)
{
if(low<high)
{
int middle =(low+high)/2;
MSort(H,low,middle);
MSort(H,middle+1,high);
Merge(H,low,middle,high);
}
}
void MergeSort(int *H,int length)
{
MSort(H,0,length-1);
}
int main()
{
int H[] = {49,38,65,97,76,13,27,49};
MergeSort(H,8);
copy(H,H+8,ostream_iterator<int ,char>(cout," "));
cout<<endl;
}