# Sort an Array of 0s and 1s

“** Sort an Array of 0s and 1s**” is one of the favorite technical interview problem of array data structure. Here, we are given an array of size ‘n’ and our task is to sort this array.

Array contains only 0s and 1s.

**Example:**

Arr[5] = {1, 0, 1, 0, 1}INPUT:Arr[5] = {0, 0, 1, 1, 1}OUTPUT:

There are many methods to sort an array of 0s and 1s. Some of them are:

**METHOD 1: Brute-force Approach to Sort an Array of 0s and 1s**

The brute-force solution of the given problem is to simply sort the array using quick sort or merge sort.

The time complexity of the above problem would be O(nlogn) for sorting.

**METHOD 2: Count and Replace Approach to Sort an Array of 0s and 1s**

This method is more time efficient and requires O(n) time complexity with traversing the array two times only.

In this method, we count the occurrence of 0s as count, and then, we put 0s at the beginning and rest 1s in the array.

**C++ Program to sort an array of 0s and 1s is as follows:**

```
/* Program to sort an array of 0s and 1s */
#include<bits/stdc++.h>
using namespace std;
int main()
{
/* Scan array size */
int n;
cout<<"Enter array size:";
cin>>n;
/* Scan array elements */
int arr[n];
cout<<"\nEnter array elements:\n";
for(int i = 0; i < n; i++)
cin>>arr[i];
/* Count 0s */
int count0 = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] == 0)
count0++;
}
/* we put 0s at the beginning and rest 1s in the array */
for(int i = 0; i < count0; i++)
{
arr[i] = 0;
}
for(int i = count0; i < n; i++)
{
arr[i] = 1;
}
/* Print the resultant array */
cout<<"\nThe sorted array is:\n";
for(int i = 0; i < n; i++)
cout<<arr[i]<<" ";
}
```

Enter array size: 5 Enter array elements: 0 1 0 1 0 The sorted array is: 0 0 0 1 1OUTPUT:This requires two times traversal of the array. O(n), where n is the size of the arrayTime Complexity:

**METHOD 3: Two Pointers**

The steps required to sort an array of 0s and 1s are as follows:

- Scan array size.
- Scan array elements.
- Set ptr1 = 0 and ptr2 = n-1
- While(ptr1 < ptr2)
- while(arr[ptr1] == 0) then ptr1++.
- while(arr[ptr2] == 1) then ptr2–.
- If(ptr1 < ptr2) then swap(arr[ptr1],arr[ptr2]) and ptr1++ ,ptr2–.

**C++ Program to sort an array of 0s and 1s is as follows:**

```
/* Program to sort an array of 0s and 1s */
#include<bits/stdc++.h>
using namespace std;
int main()
{
/* Scan array size */
int n;
cout<<"Enter array size:";
cin>>n;
/* Scan array elements */
int arr[n];
cout<<"\nEnter array elements:\n";
for(int i = 0; i < n; i++)
cin>>arr[i];
/* Initiate pointers */
int ptr1 = 0, ptr2 = n-1;
while(ptr1 < ptr2)
{
/* Increment ptr1 till we get 0s */
while(arr[ptr1] == 0)
ptr1++;
/* Decrement ptr2 till we get 1s */
while(arr[ptr2] == 1)
ptr2--;
/* swap elements */
int temp = arr[ptr1];
arr[ptr1] = arr[ptr2];
arr[ptr2] = temp;
ptr1++;
ptr2--;
}
/* Print the resultant array */
cout<<"\nThe sorted array is:\n";
for(int i = 0; i < n; i++)
cout<<arr[i]<<" ";
}
```

Enter array size: 5 Enter array elements: 0 1 0 1 0 The sorted array is: 0 0 0 1 1OUTPUT:This method requires only one time traversal of array O(n), where n is the size of the array.Time Complexity:

**Related Posts:**

- Program to Concatenate Two Character Arrays.
- Program to Check whether given String is Palindrome or not.
- Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
- Program to count total number of words in a string.
- Program to find smallest and largest word of the String.
- Program to find Most Frequent Character of the String.
- Program to Remove all the blank spaces from the String.
- Program to check if String is isogram or not.
- Program to Reverse Each word of the String.
- Program to Print All the Substring of the Given String.
- Program to find longest palindromic Substring.
- Detect a Loop in a Linked List.
- Find the Length of the Loop present in the Linked List.
- Detect and Remove Loop from a Linked List.
- Segregate Even and Odd Nodes in a Linked List.
- Delete Complete Linked List.
- Delete Nth Node of the Linked List.
- Delete without head pointer of the Linked List.
- Delete All Occurrences of particular node of the Linked List.
- Delete Alternate Nodes of the Linked List.