-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.C
71 lines (57 loc) · 1.72 KB
/
MergeSort.C
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
#include <stdlib.h> /* srand, rand */
void MergeSort(){
const int arraysize=pow(2,8);
std::vector<int> merge(std::vector <int>, std::vector <int>);
int nexp=log(arraysize)/log(2);
std::vector<int> original;
int narray=0;
for (int i=0;i<=nexp;i++){
narray=narray+int(pow(2,i));
}
std::vector<int> splitted[narray];
for (int i=0;i<arraysize;i++){
original.push_back(rand()%arraysize);
}
for (int i=0;i<arraysize;i++){
splitted[i].push_back(original[i]);
}
int counter=arraysize;
int index=0;
for (int i=nexp-1;i>=0;i--){
for(int j=0;j<pow(2,i);j++){
cout<<"index "<<index<<" and "<<index+1<<" goes to="<<arraysize<<endl;
splitted[arraysize]=merge(splitted[index],splitted[index+1]);
arraysize++;
index=index+2;
}
}
cout<< arraysize<<endl;
for (std::vector<int>::iterator it = splitted[arraysize-1].begin() ; it != splitted[arraysize-1].end(); ++it) std::cout << ' ' << *it;
}
std::vector<int> merge(std::vector <int> a, std::vector <int> b){
std::vector <int> arraymerged;
int size_a=a.size();
int size_b=b.size();
if (size_a!=size_b) {cout<<"error in the size of the arrays"<<endl; return;}
arraymerged.reserve(size_a*2);
int index_a=0;
int index_b=0;
for (int i=0; i<2*size_a;i++){
if(index_a==size_a) {
for (int m=index_b; m<size_a;m++) arraymerged.push_back(b[m]);
return arraymerged;
}
if(index_b==size_a) {
for (int m=index_a; m<size_a;m++) arraymerged.push_back(a[m]);
return arraymerged;
}
if(a[index_a]<=b[index_b]){
arraymerged.push_back(a[index_a]);
index_a++;
}
else{
arraymerged.push_back(b[index_b]);
index_b++;
}
}
}