Sort an Array of 0s, 1s and 2s

Given an array consisting of 0s, 1s, and 2s. Write code to sort an array of 0s, 1s, and 2s without using an extra space or a sorting algorithm.

OR

Given an array of size n containing only 0s, 1s, and 2s. Write a code to sort the array in ascending order.

For example:

Input  : {0, 1, 1, 0, 1, 2, 0, 1, 2}

Output : {0, 0, 0, 1, 1, 1, 1, 2, 2}

In this example, the input array only consists of 0s, 1s, and 2s in an unsorted order.

In the output array, all the 0s, 1s and 2s are in sorted order.

The easiest version of this problem is to segregate 0s and 1s in an array and Move all zeros to end of array. This problem is also known as the Dutch national flag problem.

In this tutorial, I am going to discuss multiple approaches and their time and space complexities to solve this problem. Also, We’ll write the java code for all the approaches.

Also, at the end of this tutorial, I have shared the link to the video tutorial.

Sort an array of 0s, 1s and 2s

Programming video tutorials

How to Sort an Array of 0s, 1s and 2s?

Let’s start with the simplest approach. The simplest solution is to sort an array. Once the array is sorted the number (0s, 1s, 2s) are arranged in sorted order.

The time complexity of sorting an array is O(nlogn).

If you are not familiar with the sorting algorithm then check different sorting algorithms and their time complexities

Our problem is already solved. As per the problem statement, we have to arrange the 0s, 1s, and 2s in sorted order which we have done through sorting.

Can we further improve the time complexity of our solution? Yes, we can solve this problem in O(n) time complexity.

Sort an Array of 0s, 1s, 2s using Simple Counting – Java Code

The idea here is to count the number of zero and one present in an array. Once we know the count of zero and one we can easily find the count of two by subtracting the count of zero and one with an array length.

Now, we know the count of zero, one, and two present in an array. To arrange the 0s, 1s, and 2s in ascending order, we need to first put 0s than 1s, and after that 2s in an array.

The time complexity of this approach is O(n).

Sort a linked list of 0s, 1s and 2s

Sort an Array of 0s, 1s, and 2s (In-place sorting)

The algorithm we are going to use in this example is also known as the Dutch national flag algorithm or three-way partitioning. The idea here is to sort an array of 0s, 1s, 2s in a single traversal without using extra space or a sorting algorithm.

Here are the following steps of this algorithm.

i) Declare three variables start=0, mid=0, high= n-1 where n is the length of an array.

ii) Run a loop until mid<=high and consider three cases on the basis of the value of array [mid].

  • If the value array[mid] is 0.
    Swap (array [start], array [mid])
    Increment low
    Increment mid
    Break statement
  • If the value of array[mid] is 1. Keep as it is since 1’s should be at the middle of a sorted array.
    Increment mid
    Break statement
  • If array[mid] is 2.
    Swap (array [mid], array [high])
    Decrement high
    Break statement

The time complexity of this approach is O(n) without using any extra space.

Sort an Array of 0s, 1s and 2s – Video Tutorial

In this tutorial, I have explained how we can segregate an array of 0s, 1s, and 2s using simple counting. Count the number of 0s, 1s, and 2s present in an array and then put them in sorted order.

Sort an array of 0s, 1s, and 2s video tutorial

Data structure and algorithms made easy

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.