-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedium_Prob143.cpp
More file actions
53 lines (44 loc) · 1.25 KB
/
Medium_Prob143.cpp
File metadata and controls
53 lines (44 loc) · 1.25 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
/* Given a pivot x, and a list lst, partition the list into three parts.
The first part contains all elements in lst that are less than x
The second part contains all elements in lst that are equal to x
The third part contains all elements in lst that are larger than x
Ordering within a part can be arbitrary. */
#include <iostream>
#include <vector>
using namespace std;
void partition(vector<int>& list, int pivot){
vector<int> part1 ;
vector<int> part2 ;
vector<int> part3 ;
for (int i=0; i<list.size(); i++){
if (list[i] < pivot){
part1.push_back(list[i]);
} else if(list[i] == pivot){
part2.push_back(list[i]);
} else {
part3.push_back(list[i]);
}
}
cout << "Part 1 : ";
for (int i=0; i<part1.size(); i++){
cout << part1[i] << " ";
}
cout << "\n" << "Part 2 : ";
for (int i=0; i<part2.size(); i++){
cout << part2[i] << " ";
}
cout << "\n" << "Part 3 : ";
for (int i=0; i<part3.size(); i++){
cout << part3[i] << " ";
}
cout << "\n";
}
int main(int argc, char *argv[])
{
vector<int> list ;
int n = 10;
for (int i=0; i<n; i++){
list.push_back(n-i);
}
partition(list, 4);
}