Sort an array containing two types of elements


There are different approaches to sort an array containing only two types of elements i.e., only 1’s and 0’s. We will discuss three different approaches to do so. First approach simply uses a predefined sort() function to sort the given array. Second approach is a count sort approach in which we will count the number of zeroes and ones and then update the array by first writing zero for the number of times 0 was counted and then writing 1’s for the number of times we counted one. In the last approach, we used the two pointer method.

Problem statement

We are given an array containing only 1’s and 0’s. Our task is to arrange all the 0’s and 1’s such that 1’s are on rightmost side of the array and 0’s are to the left side of the array

Example

Given array: a[] = [ 1, 1, 0, 1, 0, 1, 0, 1 ]
Resultant array: a[] = [ 0, 0, 0, 1, 1, 1, 1, 1 ]

Approach 1: Brute Force method.

In this method, we will directly sort the array using sort() function. Since 1>0, after sorting, all the 1’s will be arranged in the right side of the array and all 0’s will be arranged towards the left side.

Sort() Function: Sort() functions take O(NlogN) time and returns the array in ascending order.

Example

Below is a C++ implementation of the above approach to sort an array containing two types of elements.

#include <bits/stdc++.h>
using namespace std;
int main(){
   int a[] = { 1, 1, 0, 1, 0, 1, 0, 1 };
   int len = sizeof(a) / sizeof( a[0] );
   
   // sort function to
   // sort the array
   sort( a, a + len);
   cout<< " After sorting the array, the array obtained is ";
   for(int iterator =0; iterator<len; iterator++){
      cout << a[iterator] <<" ";
   }
   return 0;
}

Output

On compilation, the above program will produce the following output −

After sorting the array, the array obtained is 0 0 0 1 1 1 1 1

Approach 2: Count sort approach

In this approach, we will simply count the number of 0’s and 1’s in the array and then update the array with 0’s to the beginning and 1’s in the last.

By doing this, we will have all the 0’s in the leftmost part of the array and all the 1’s to the rightmost part of the array which will automatically represent the sorted array required.

This is an easy approach but it inserts new values to the array instead of just swapping their positions due to which, this approach is not efficient.

Example

Below is an implementation of the above approach using C++.

#include <iostream>
using namespace std;
int main() {
   int count = 0 ;
   int a[] = { 1, 1, 0, 1, 0, 1, 0, 1 };
   int len = sizeof(a) / sizeof(a[0]);
   for(int iterator=0; iterator<len; iterator++){
      if( a[iterator] == 0 )
      count++;
   }
   for(int iterator=0 ; iterator<len ; iterator++){
      if(count){
         a[iterator] = 0 ;
         count-- ;
      }
      else a[iterator] = 1;
   }
   cout<< " After sorting the array, the array obtained is ";
   for(int iterator =0 ; iterator<len ; iterator++){
      cout<< a[iterator] << " ";
   }
   return 0;
}

Output

On compilation, it will produce the following output −

After sorting the array, the array obtained is 0 0 0 1 1 1 1 1

Time complexity − Since we are using a single loop, the time complexity for this approach is O(N)

Space Complexity − O(1). Although we have used extra variables but since they are linear, the space complexity is constant.

Approach 3: Best approach to solve this problem

In this approach, we will keep 2 pointers, start and end. We will traverse the array from beginning (index 0) using start pointer and from end (index = len -1 ) using end pointer simultaneously.

Our task is to arrange all the 1’s to the rightmost side of the array which will result in all the 0’s to the left of the array automatically.

We start with the initial index i.e., start and if element found is 1, we will swap it with element at the index “end” and decrement the end pointer by 1.

If the element found is a 0, then there is no need to perform any swap operation as it is already at its leftmost position, we will just increment the start pointer by 1.

Example

Code for this approach is given below −

#include <bits/stdc++.h>
using namespace std;
int main(){
   int start =0 ;
   int a[] = { 1, 1, 0, 1, 0, 1, 0, 1 } ;
   int len = sizeof(a) / sizeof( a[0] ) ;
   int end = len-1;
   while(start < end){
      if( a[start] ==1){
         swap(a[start], a[end]);
         end--;
      }
      else
         start++;
   }
   cout<< " After sorting the array, the array obtained is ";
   for(int iterator =0 ; iterator<len ; iterator++){
      cout<< a[iterator] << " ";
   }
   return 0;
}

Output

After sorting the array, the array obtained is 0 0 0 1 1 1 1 1

Time Complexity − As we traversed the array only once, the time complexity for this approach is linear i.e., O(N)

Space Complexity − As we have not used any additional space, the space complexity is O(1).

Conclusion

In this article, we discussed three different approaches to sort an array containing only two types of elements i.e., only 1’s and 0’s.

Updated on: 11-Apr-2023

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements