Examples:

Input:

arr[ ] = {1, 1, 0, 1, 0, 0, 0, 1, 1, 0}

Output:

arr[ ] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1}

#include <bits/stdc++.h>
using namespace std;

// function to sorting 0 and 1
void sortFun(int array[], int n)
{
    int l = 0;
    int r = n - 1;

    // loop until l is less than r
    while (l < r)
    {

        if (array[l] == 1)
        {
            // swap element at l and r
            swap(array[l], array[r]);
            r--;
        }
        else
        {
            l++;
        }
    }
}

// Function to print the array
void printArray(int array[], int n)
{
    for (int i = 0; i < n; i++)
        cout << array[i] << " ";
    cout << endl;
}

// Main function
int main()
{
    int array[100], n;
    cout << "Enter Number of elements: ";
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> array[i];
    }

    // sorting the array
    sortFun(array, n);

    cout << "Sorted Array" <<endl;
    printArray(array, n);

    return 0;
}

Output:

Enter Number of elements: 8
1 0 1 0 1 1 0 0
Sorted Array
0 0 0 0 1 1 1 1