-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuickSort.cpp
65 lines (51 loc) · 1.8 KB
/
QuickSort.cpp
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
#include <stdio.h>
#include <vector>
#include <iostream>
//main sort function
std::vector<int> quick_sort(std::vector <int> &vector__);
//connection of greater, lower and middle arrays
std::vector<int> connect(std::vector <int> start__, std::vector <int> end__, int number__);
void print(std::vector <int> &vector__);
int main(){
std::vector <int> array {9, 8, 17, 22, 1, 25};
std::vector <int> sorted_array;
print(array);
sorted_array = quick_sort(array);
print(sorted_array);
}
void print(std::vector <int> &vector__){
printf("Printing String");
for (int i=0; i<vector__.size(); i++){
printf("%d ", vector__[i]);
}
}
std::vector<int> quick_sort(std::vector <int> &vector__){
int length = vector__.size();
int id_of_element = length/2;
int number__ = vector__[id_of_element];
std::vector <int> less__;
std::vector <int> greater__;
print(vector__);
if (length < 2){
return vector__;
}else{
for (int i = 0; i<=length-1; i++){
if (vector__[i] <= vector__[id_of_element] && i != id_of_element){
less__.push_back(vector__[i]);
}else if(vector__[i] > vector__[id_of_element]){
greater__.push_back(vector__[i]);
}
print(greater__);
}
return connect(quick_sort(less__), quick_sort(greater__), number__);
}
}
std::vector<int> connect(std::vector <int> start__, std::vector <int> end__, int number__){
std::vector <int> middle__ {number__};
std::vector <int> output__;
print(start__);
output__.insert(output__.begin(), start__.begin(), start__.end());
output__.insert(output__.end(), middle__.begin(), middle__.end());
output__.insert(output__.end(), end__.begin(), end__.end());
return output__;
}