Disclaimer: This is a user generated content submitted by a member of the WriteUpCafe Community. The views and writings here reflect that of the author and not of WriteUpCafe. If you have any complaints regarding this post kindly report it to us.

Do you know?

An array is touted as a linear data structure containing all elements in their respective contiguous memory locations. 

This is the most sought after and a highly recommended DSA concept! 

You can’t really take a chance of leaving array related questions for your interview preparation as this is the favourite topic of most interviewers. 

From all array related problems like sorting array,  largest subarray with 0 sum the question on sort array of 0 1 2 is commonly asked by the interviewers.  Though, most of the candidates don’t have adequate knowledge on this topic. 

Hence, to equip you with the right knowledge of sorting algorithm, we have curated this blog. 

Learn all the nits and grits of how to sort an array of 0s, 1s, and 2s in linear time!


What is sorting?

Sorting is defined as the ordering of data in either ascending or descending order based on the linear relationship of these data items. 

In an array sorting plays a key role as we can insert the elements, merge them, fix their place or move them based on the problem statement. You can sort the array elements based on their specific order. 

In this blog we are going to look at how to sort arrays of 0 , 1, and 2 in the linear fashion. 


What do you know about arrays of 0s, 1s and 2s?

As we already mentioned, an array is a data structure used to store elements. In the 0s, 1s or 2s array, we store all the elements of the array in the form of 0s, 1s and 2s. 

If we talk about sorting array elements in the form of 0s, 1s and 2s, we can arrange our elements either in the ascending or descending order. Though, we will consider the ascending order as our default case. 

The main task we should perform to sort an array would be that all the 0s will come before the 1s and all the 1s will come before the 2s. Then all 2s will be filled in our remaining array. 

To better understand this concept, let’s have a look at the below-mentioned problem statement. 


Problem Statement 

An array of size n is given to you. This array contains only three types of values and they are 0, 1 and 2. You need to sort this array of all 0s, 1s and 2s. This means, place all zeros before the ones and ones before twos. 

Note: You don’t need to write a stable sort algorithm. This means that arranged 0s can be written in any order, arranged ones can be written in any order and arranged twos can also be written in any order. 

Example – If your array is [ 0,1,2,0,2,1,0,2,1], your final sorting array would be;

[ 0,0, 0,1,1,1,2,2,2] 

This example would give you a better conceptual clarity on how to sort an array of 0s, 1s and 2s. 


Methods to sort arrays of 0s, 1s and 2s

You can follow certain methods in order to sort an array of 0s, 1s and 2s. For this you need to have a better understanding of all these methods. 

Let’s know about them in detail:


Using brute force approach with double traversal

To sort an array of 0,1,2, you can use brute force method which is considered among one of the most efficient methods. 

With the help of this approach, you can sort your array of 0s, 1s and 2s by arranging them in the ascending order. However, it totally depends on a programmer, if he wants to sort an array in either ascending or descending order. 

You can sort the array in the default case which is in the ascending order. 

Take the example of sorting arrays of 0s, 1s and 2s

0 2 1 0 2 1 0 2 

0 0 0 1 1 2 2 2 


We can easily derive the algorithm for our n number of elements by taking a random array and doing our experiment on it. 

A will be; [ 0, 1, 0, 2, 1, 1, 0, 2, 2, 0, 1] 


Procedural steps to follow are:

  • Count all numbers of elements in an array. Here our array contains total 11 elements
  • Scan these elements from left to right and trace them step by step 
  • Count number of 0s present and store them as your variable
  • Count number of 1s present and store them as your variables
  • Count number of 2s present and store them as your variables
  • You can do this because you know the elements present in an array. If you do in the runtime you are not sure if the values entered are in this form or not. 
  • After counting all values, copy them in array A
  • For copying all the values, start loop from the 0th index and copy all 0s, all 0s will be copied till index 3
  • Then to copy all s start from the index 4. To copy 1 simultaneously increase your index step by step
  • To copy 2s in an array start from the index 8 and move till index 7. To copy all 2s, increase index step by step
  • In the end, we will succeed in copying all 0s, 1s and 2s


Algorithm for this approach would be:

  • Consider array of element n with all 0s, 1s and 2s
  • Construct a sorting function of 0s, 1s and 2s
  • Pass number of elements, declare all variables of zero, one and two count
  • After counting all 0s, 1s and 2s, insert values back in your array
  • Put all 0s back in array to pass the argument 
  • After putting zeros from their continue index numbers, insert ones in an array
  • Put all the 2s back in the remaining indexes
  • In the main function, print your sorted array 


Wrapping Up 

We hope that with the above mentioned problem statement and approaches, you will have a better idea on how to sort array of 0 1 2.

Further, you can also enhance your knowledge on the related array concepts like largest subarrays with 0 sum. 

Happy learning!



Welcome to WriteUpCafe Community

Join our community to engage with fellow bloggers and increase the visibility of your blog.
Join WriteUpCafe