-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique.cc
More file actions
76 lines (70 loc) · 1.67 KB
/
unique.cc
File metadata and controls
76 lines (70 loc) · 1.67 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
76
#include <algorithm>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
const int NUMELEMS = 20;
void
swap(vector<int>& vi, uint32_t first, uint32_t second)
{
auto tmp = vi[first];
vi[first] = vi[second];
vi[second] = tmp;
}
int
partition(vector<int> &vi, int start, int end)
{
int pivot = vi[end];
size_t i = start;
size_t j = end - 1;
while (i < j) {
if (vi[i] < pivot) {
i++;
continue;
} else {
swap(vi, i, j);
j--;
}
}
if (vi[i] < pivot) {
swap(vi, i + 1, end);
return i + 1;
} else {
swap(vi, i, end);
return i;
}
}
void
quickSort(vector<int> &vi, int start, int end)
{
if (start >= end)
return;
auto pivotPos = partition(vi, start, end);
quickSort(vi, start, pivotPos - 1);
quickSort(vi, pivotPos + 1, end);
}
int
main (int argc, char *argv[])
{
srand(time(NULL));
vector<int> vi;
for (size_t i = 0; i < NUMELEMS; i++)
{
vi.push_back(rand() % 100);
}
cout << "Input data" << endl;
for_each(vi.begin(), vi.end(), [](int x) { cout << x << " "; });
cout << endl;
quickSort(vi, 0, NUMELEMS - 1);
cout << "Sorted data" << endl;
for_each(vi.begin(), vi.end(), [](int x) { cout << x << " "; });
cout << endl;
auto itr = unique(vi.begin(), vi.end());
cout << "Size after running unique: " << vi.size() << endl;
cout << "Output" << endl;
for_each(vi.begin(), itr, [](int x) { cout << x << " "; });
cout << endl;
cout << "Full data" << endl;
for_each(vi.begin(), vi.end(), [](int x) { cout << x << " "; });
cout << endl;
}