using System;
using System.Collections.Generic;
class Program
{
// Function to sort a list containing only 0s and 1s
static void SortBinaryList(List<int> arr) {
int left = 0; // Index to track the left side
int right = arr.Count - 1; // Index to track the right side
while (left < right) {
// If the left Index is at 0, move it forward
if (arr[left] == 0) {
Console.WriteLine("left: " + left);
left++;
}
// If the right Index is at 1, move it backward
else if (arr[right] == 1) {
Console.WriteLine("right: " + right);
right--;
}
// If left is 1 and right is 0, swap them
else {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
Console.WriteLine("swap() left: " + left + " right: " + right);
left++;
right--;
}
}
}
static void Main()
{
// Input: Binary list
List<int> arr = new List<int> { 1, 0, 1, 0, 1, 0, 0, 1, 0 };
// Sort the binary list
SortBinaryList(arr);
// Output the sorted list
Console.Write("Sorted list: ");
foreach (int num in arr) {
Console.Write(num + " ");
}
}
}
/*
run:
swap() left: 0 right: 8
left: 1
right: 7
swap() left: 2 right: 6
left: 3
swap() left: 4 right: 5
Sorted list: 0 0 0 0 0 1 1 1 1
*/