-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesort.h
More file actions
75 lines (58 loc) · 2.2 KB
/
mergesort.h
File metadata and controls
75 lines (58 loc) · 2.2 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
//
// Created by lenovo on 8.12.2022.
//
#ifndef DATABASES_PROJECT_MERGESORT_H
#define DATABASES_PROJECT_MERGESORT_H
#include <iostream>
#include <vector>
#include <chrono>
#include <fstream>
std::vector<int> mergesort(std::vector<int> &array);
std::vector<int> merge(std::vector<int> &left, std::vector<int> &right);
std::vector<int> mergesort(std::vector<int> &array) {
if (array.size() == 0 or array.size() == 1) {
return array;
}
//divide in two vectors
std::vector<int> left;
std::vector<int> right;
left.reserve(array.size()/2);
right.reserve(array.size()/2);
for (int i = 0; i < array.size(); ++i) {
if (i < array.size() / 2) {
left.push_back(array[i]);
} else {
right.push_back(array[i]);
}
}
std::vector<int> merge1 = mergesort(left);
std::vector<int> merge2 = mergesort(right);
std::vector<int> mergedArray = merge(merge1, merge2);
return mergedArray;
}
std::vector<int> merge(std::vector<int> &left, std::vector<int> &right) {
//check the first elements, see which one is smaller
int refIndexL = 0;
int refIndexR = 0;
std::vector<int> output; //output should be the size of the two vectors combined
output.reserve(left.size() + right.size());
while (refIndexL < left.size() and refIndexR < right.size()) { //while left and right is not empty
if (left[refIndexL] <= right[refIndexR]) { //if left size is smaller than right size
output.push_back(left[refIndexL]); //push back
refIndexL++;
// left.erase(left.begin()); //we erase it, we can keep checking the first element
} else {
output.push_back(right[refIndexR]);
//right.erase(refIndexR); //erase doesn't work we have to change it reference point index and move the index until it reaches the size of the array
refIndexR++;
}
}
for(int i=refIndexL; i < left.size(); ++i) {
output.push_back(left[i]);
}
for(int i=refIndexR; i<right.size(); ++i) {
output.push_back(right[i]);
}
return output;
}
#endif //DATABASES_PROJECT_MERGESORT_H