Javatpoint Logo
Javatpoint Logo

Write the Python Program to Sort the List of 0s, 1s and 2s

In this tutorial, we write the program to sort the list of the 0s, 1s and 2s. Here the meaning of list of 0s, 1s, and 2s is the list consists of data only 0s, 1s, and 2s form. For example, an array containing 11 elements and these elements are as follows - 0, 1, 1, 2, 2, 1, 0, 2, 1, 0, 0.

Problem Statement

The problem is given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order. For example -

Example - 1:

Example - 2:

Solution

We will solve this problem using the following approaches.

  • Brute Force Approach
  • The Dutch National Flag Algorithm

Let's understand the brute force approach.

Approach - 1:

To solve this problem using the brute-force, we will use the bubble sort algorithm. Let's understand the following code snippet.

Example -

Output:

The Given Array is: [2, 0, 2, 1, 1, 0]
The Sorted Array is: [0, 0, 1, 1, 2, 2]

Explanation -

In the above code, we define the sort_array() function that takes an input array "arr" as its argument and returns a sorted version of the input array.

The function uses the Bubble Sort algorithm to sort the array. The basic idea of the algorithm is to repeatedly compare adjacent elements in the array and swap them if they are in the wrong order. This process is repeated until the entire array is sorted.

The function starts by determining the length of the input array using the built-in "len" function and assigning it to the variable "n". It then enters a nested loop structure using two "for" loops to iterate over each element in the array. The outer loop iterates over each element of the array from the first element to the last element, while the inner loop iterates over all elements from the first element to the last unsorted element.

Within the nested loops, the function compares adjacent elements in the array using an "if" statement to check if the element on the left is greater than the element on the right. If this condition is true, it swaps the two elements using a tuple assignment statement.

The arr[j], arr[j + 1] = arr[j + 1], arr[j] statement is a shorthand way of swapping the values of arr[j] and arr[j+1]. It creates a temporary tuple with the two values to be swapped, then unpacks the tuple and assigns the values to their respective variables.

After the nested loops have completed, the function returns the sorted input array using the "return" statement. The final output will be the same array, but with its elements sorted in ascending order.

Approach - 2:

To sort the 0s, 1s, and 2s, we will use the Dutch National Flag Algorithm which is quit efficient algorithm to solve the array of distinct elements. Let's understand the following example.

Example -

Output:

The Given Array is: [2, 0, 2, 1, 1, 0]
The Sorted Array is: [0, 0, 1, 1, 2, 2]

Explanation -

In the above code, we define the sort_arr() function, which takes list of 0s, 1s and 2s and return the sorted list. First, we initialize the three pointers "low", "mid", and "high", which are used to keep track of the positions of the 0s, 1s, and 2s in the array.

The while loop continues until the "mid" pointer reaches the end of the array. Within the loop, we have three cases to consider:

  • If the element at position "mid" is 0, we swap it with the element at position "low" and increment both "low" and "mid".
  • If the element at position "mid" is 1, we don't need to do anything and simply increment "mid".
  • If the element at position "mid" is 2, we swap it with the element at position "high" and decrement "high".

After the loop has finished, the array will be sorted in ascending order with all 0s at the beginning, followed by all 1s, and then all 2s. We return the sorted array using the "return" statement.

This algorithm has a time complexity of O(n) where n is the length of the input array, and a space complexity of O(1) as it sorts the array in place without using any extra space.

Conclusion

In this tutorial, we solved the list of 0s, 1s and 2s using the two approaches - first approach we use the bubble sort algorithm whose time complexity is 0(N2). In the second approach, we implemented the Dutch National Flag Algorithm which is more efficient than the bubble sort as its time complexity is 0(N).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA