-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD16_sorting.cpp
More file actions
60 lines (50 loc) · 1.63 KB
/
D16_sorting.cpp
File metadata and controls
60 lines (50 loc) · 1.63 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
// Given an array arr[] containing only 0s, 1s, and 2s. Sort the array in ascending order.
// Examples:
// Input: arr[] = [0, 1, 2, 0, 1, 2]
// Output: [0, 0, 1, 1, 2, 2]
// Explanation: 0s 1s and 2s are segregated into ascending order.
// Input: arr[] = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
// Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2]
// Explanation: 0s 1s and 2s are segregated into ascending order
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void sort012(vector<int>& arr) {
int low = 0, mid = 0, high = arr.size() - 1;
while (mid <= high) {
if (arr[mid] == 0) {
// Swap arr[low] and arr[mid], move both pointers
swap(arr[low], arr[mid]);
low++;
mid++;
}
else if (arr[mid] == 1) {
// If the element is 1, just move the mid pointer
mid++;
}
else { // arr[mid] == 2
// Swap arr[mid] and arr[high], move the high pointer
swap(arr[mid], arr[high]);
high--;
}
}
}
};
int main() {
Solution solution;
vector<int> arr1 = {0, 1, 2, 0, 1, 2};
solution.sort012(arr1);
for (int num : arr1) {
cout << num << " ";
}
cout << endl; // Output: 0 0 1 1 2 2
vector<int> arr2 = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};
solution.sort012(arr2);
for (int num : arr2) {
cout << num << " ";
}
cout << endl; // Output: 0 0 0 0 0 1 1 1 1 1 2 2
return 0;
}