Sort Colours Problem29 Aug 2024 | 9 min read In the sort colours problem, we are given an array with three numbers. Let's say the numbers are 0s, 1s, and 2s. Our task is to write a program to sort these numbers. After sorting the arrays, the arrays will contain all 0s first, then 1s and at last, 2s. Let us see some examples to understand the problem: Input: [0, 1, 0, 0, 1, 2, 0, 1, 1, 2] Output: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2] Input: [0, 1, 0, 2, 1, 2, 1, 1, 0, 1, 1, 2, 0, 0, 1, 0, 1] Output: [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2] Brute Force ApproachThe brute force approach will be to use any sorting algorithm to sort the array. The average time complexity of a good sorting algorithm is log n, where n is the size of the given array. However, we can optimize the time complexity to linear time complexity. Approach - 1We will solve this problem using two pointers: The array can be divided into four sections which will store the three colours 0s, 1s, and 2s. We will initiate three pointers l = 0, m = 0 and h = last index of the array
We will perform three operations based on the ith element:
Let us see an example to understand how this program will work array = [1, 0, 1, 2, 0, 1, 1] l = 0, m = 0, h = 6 Step - 1: array[m] == 1 m = m + 1 = 1 array = [1, 0, 1, 2, 0, 1, 1] Step - 2: array[m] = 0 swap(array[l], array[m]) m = m + 1 = 2 l = l + 1 = 1 array = [0, 1, 1, 2, 0, 1, 1] Step - 3: array[m] == 1 m = m + 1 = 3 array = [0, 1, 1, 2, 0, 1, 1] Step - 4: array[m] == 2 swap(array[m], array[h]) h = h - 1 = 5 array = [0, 1, 1, 0, 1, 1, 2] Step - 5: array[m] == 0 swap(array[l], array[m]) m = m + 1 = 4 l = l + 1 = 2 array = [0, 0, 1, 1, 2, 2] Step - 6: array[m] == 2 swap(array[m], array[h]) h = h - 1 = 4 array = [0, 0, 1, 1, 2, 2] Step - 7: array[m] == 2 swap(array[m], array[h]) h = h - 1 = 3 array = [0, 0, 1, 1, 2, 2] Hence, the sorted array is array = [0, 0, 1, 1, 2, 2] Below are the steps to be followed to solve this problem:
CodeOutput: The sorted array is: 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 Time Complexity: Since we are using a linear loop, the time complexity of this approach is O(n). Space Complexity: Since we have modified the original array, the space complexity of this approach is constant, i.e., O(1). Approach - 2In this approach, we will use the counting method to sort the array.
Let us see an example to understand the solution: array = [0, 1, 0, 1, 2, 0, 1, 1, 2] count0 = 0, count1 = 0, count2 = 0 At n = 0: array[0] == 0 count0 += 1 # count0 = 1 At n = 1: array[1] == 1 count1 += 1 # count1 = 1 At n = 2: array[2] == 0 count0 += 1 # count0 = 2 At n = 3: array[3] == 1 count1 += 1 # count1 = 2 At n = 4: array[4] == 2 count2 += 1 # count2 = 1 At n = 5: array[5] == 0 count0 += 1 # count0 = 3 At n = 6: array[6] == 1 count1 += 1 # count1 = 3 At n = 7: array[7] == 1 count1 += 1 # count1 = 4 At n = 8: array[8] == 2 count2 += 1 # count2 = 2 # Create an array containing count0 times 0, count1 times 1 and count2 times 2. Hence, the sorted array is [0, 0, 0, 1, 1, 1, 1, 2, 2]. We will follow the following steps to solve the problem using the approach shown above.
Below is the Python program for the above approach. Code Output: The sorted array is: 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 Time Complexity: We have not used any nested loop in this program. Only linear loops are used. Therefore, the time complexity is also linear, i.e., O(N). Space Complexity: We have created an array to store the sorted array. Hence, the space complexity is linear, i.e., O(N). Approach - 3We can modify the above-mentioned approach and reduce the space complexity to constant. After finding the counts of 0s, 1s, and 2s, we will follow these steps instead of creating a new array:
array = [0, 0, 0, 1, 2, 0, 1, 1, 2]
array = [0, 0, 0, 1, 1, 1, 1, 1, 2]
array = [0, 0, 0, 1, 1, 1, 1, 2, 2] Hence, the sorted array is [0, 0, 0, 1, 1, 1, 1, 2, 2]. Hence, the last step of the algorithm will be to run three while loops where the number of iterations will be equal to the value of the respective counts and store the 0s, 1s, and 2s in the original array. Below is the Python program for the above-mentioned algorithm. Code Output: The sorted array is: 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 Time Complexity- Same as before, O(N). Space Complexity: We have updated the given array; hence we have not used any extra space to store the sorted array. Therefore, the space complexity is constant, i.e., O(1). Next TopicJump Game Problem in Python |
? We can integrate systems more efficiently and work very quickly with the help of the Python programming language. Python is a high-level, widely used general-purpose programming language, and it was designed with an emphasis on improving the readability of the code. The syntax of Python allows...
5 min read
In this tutorial, we will learn how to create different types of hollow pyramid patterns using Python. Program 1: Program to make a simple hollow pyramid in Python Code: def hollow_pyramid( r ) : m = 0 for n in range(1, r +...
6 min read
? The "hex" is an abbreviation for Hexadecimal. It is a numbering system that uses 16 as its base. It is commonly used in computing and digital electronics because it can represent a byte (8 bits) of data with just two digits, which makes it more concise...
3 min read
In this tutorial, we are discussing how to use python Web Development. Python is an adorable language. It is miles clean to look at and fun, and its syntax (the guidelines) is plain and concise. Python is a favourite choice for beginners; however still effective and...
6 min read
What is Auto clicker? Auto clicker is a program where some code script is written, and based on the code, if some user defines a key is pressed, then the mouse will be clicked automatically. In Python, we can make an auto clicker project using a pynput...
3 min read
The os.path.basename() is a method in Python's os.path module that returns the base name of a file path. The base name is the final component of the path, after stripping all parent directory and extension information. For example, if the path is /home/user/Documents/myfile.txt, the base name is...
3 min read
? Suppose you've been around the Python community for a while. In that case, you might recall conversations over Python 2 vs. Python 3, or you might have observed the release of versions like Python 3.10 and Python 3.11 amid considerable excitement. You may have observed that...
10 min read
When we get a huge number of datasets, then it will be quite beneficial to speed through the datasheet into an equal chance and then process each data frame on an individual basis. This will be only possible when the operation on the data frame is...
5 min read
The game will be enjoyable to play. Let's start building this Mad Libs Generator project in Python and learn some entertaining ideas. Mad Libs Generator Introduction It's a popular children's game. A user will be given a story and required to enter a word without knowing the...
8 min read
Infix expression: Infix expressions contain the operator in between the two operands. The operands can themselves contain operators. Though with respect to the middle operator, the expression will be an infix expression. Infix expressions are of the form (operand_1 operator oprand_2) Example: (X + Y) * (X1 + Y1) Postfix...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India