-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Harshit Pathak edited this page Dec 28, 2023
·
1 revision
Welcome to the Dutch-National-Flag-Algorithm wiki! This is the only the code in c++
#include <bits/stdc++.h> using namespace std;
void sortArray(vector& arr, int n) {
int low = 0, mid = 0, high = n - 1; // 3 pointers
while (mid <= high) {
if (arr[mid] == 0) {
swap(arr[low], arr[mid]);
low++;
mid++;
}
else if (arr[mid] == 1) {
mid++;
}
else {
swap(arr[mid], arr[high]);
high--;
}
}
}
int main() { int n = 6; vector arr = {0, 2, 1, 2, 0, 1}; sortArray(arr, n); cout << "After sorting:" << endl; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }