#include <iostream>
#include <vector>
// Function to sort a vector containing only 0s and 1s
void sortBinaryVector(std::vector<int>& arr) {
int left = 0; // Index to track the left side
int right = arr.size() - 1; // Index to track the right side
while (left < right) {
// If the left Index is at 0, move it forward
if (arr[left] == 0) {
std::cout << "left: " << left << "\n";
left++;
}
// If the right Index is at 1, move it backward
else if (arr[right] == 1) {
std::cout << "right: " << right << "\n";
right--;
}
// If left is 1 and right is 0, swap them
else {
std::swap(arr[left], arr[right]);
std::cout << "swap() left: " << left << " " << "right: " << right << "\n";
left++;
right--;
}
}
}
int main() {
// Input: Binary vector
std::vector<int> arr = {1, 0, 1, 0, 1, 0, 0, 1, 0};
// Sort the binary vector
sortBinaryVector(arr);
// Output the sorted vector
std::cout << "Sorted vector: ";
for (int num : arr) {
std::cout << num << " ";
}
}
/*
run:
swap() left: 0 right: 8
left: 1
right: 7
swap() left: 2 right: 6
left: 3
swap() left: 4 right: 5
Sorted vector: 0 0 0 0 0 1 1 1 1
*/