-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort_partition.cpp
82 lines (77 loc) · 2.07 KB
/
quicksort_partition.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// A helper function to quicksort, that may be called, if one does not want to write a special case in quicksort.
// This function basically uses the first element of the input array/vector as the pivot and places all elements smaller than
// it on it's left and those greater than it, on it's right. It makes use of two queues. Repeated call of this results in an
// array being sorted.
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> partition(vector<int> ar) {
queue<int> larger;
queue<int> smaller;
vector<int> * return_value = new vector<int>;
int partition = ar.front();
for (int i = 1; i < ar.size(); i++)
{
if (ar[i] < partition)
{
smaller.push(ar[i]);
}
else if (ar[i] > partition)
{
larger.push(ar[i]);
}
else
{
continue;
}
}
int size = smaller.size();
for (int i = 0; i < ar.size(); i++)
{
if (i < size)
{
(*return_value).push_back(smaller.front());
smaller.pop();
}
else if (i == size)
{
(*return_value).push_back(partition);
}
else
{
(*return_value).push_back(larger.front());
larger.pop();
}
}
return (*return_value);
}
int main(void) {
vector <int> _ar;
int _ar_size;
cin >> _ar_size;
for(int _ar_i=0; _ar_i<_ar_size; _ar_i++) {
int _ar_tmp;
cin >> _ar_tmp;
_ar.push_back(_ar_tmp);
}
vector<int> return_val = partition(_ar);
for (int i = 0; i < _ar_size; i++)
{
cout << return_val[i] << " ";
}
return 0;
}