# 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**

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**

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 merge two sorted arrays.****Program to find missing number in an array.****Program to sort if array is sorted.****Program to print Alternate Elements of an Array.****Program to swap kth element from beginning to kth element from end in an Array.****Program to print all possible subarrays of the given array.****Program to print kth smallest and kth largest element of an Array.****Program to find equilibrium index of an Array.****Program to find majority element of an Array.****Program to find mean of the Array.****Program to Reverse an Array.****Program to count number of odd numbers and even numbers in an array.****Program to find absolute difference between sum of odd index elements and even index elements.****Program to find smallest and largest element of the array.****Program to count occurrences of a particular element in an array.****Program to search an element in an Array (Linear Search).****Program to Rotate Array Elements by ‘D’ positions.****Program to find most frequent element in an array.****Program to find pair in an array with given sum.****Program to find first repeating element of an array.**