Javatpoint.com is changed to TpointTech.com
Sort Colours ProblemIn 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 |
Javatpoint.com is now changed to TpointTech.com, so we request you to subscribe our newsletter for further updates.
In this article, we shall see how comprehension can be done in the data structures of Python like lists, dictionaries, set, and generators. Comprehension provides a precise way of writing a program in Python. It reduces the code size without affecting its easy readability. So, here we will...
3 min read
Python is a superb language for undertaking data analysis thanks to its fantastic network of data-centric python packages. Pandas is one such application that makes importing and analysing data very simple. In pandas, there are numerous ways to replicate a DataFrame. A dataframe object can be...
3 min read
We showcase the best books for mastering Python in this tutorial from a wide variety of book reviews. Each review provides an overview of the book, including the themes addressed and the context in which writers presented those concepts. Based on the format and delivery of...
11 min read
Feed Forward Neural Network An artificial neural network that feeds information forward lacks feedback between the input and the output. A network without cyclic connections between nodes can also be used to describe it. Let's look at it as a diagram. You will see in the above Figure...
4 min read
. Palindromic numbers have always held a special fascination for mathematicians and enthusiasts alike. These are numbers that read the same backward as forward, and they have an inherently symmetric and aesthetically pleasing quality. In this article, we will explore the concept of palindromic numbers and how...
4 min read
Morphological operations can be used for extracting image components that are helpful for the description and representation of the shape of a region. Morphological operations are the fundamental tasks that are dependent on the image shape. It typically takes place on binary images. It requires two...
3 min read
In this tutorial, we will discuss how we can print the Pascal triangle using the Python program. But first, let's understand what the Pascal triangle is. Introduction Pascal triangle is an exciting concept of mathematics where a triangular array is formed by summing adjacent elements in the preceding...
5 min read
Flask Python's widely used Flask micro web framework is renowned for being straightforward and user-friendly. You can specify routes (URLs) with Flask that correlate to particular operations in our applications. Flask would execute the related function and return the outcome to the user when a user accesses...
4 min read
A matplotlib is an open-source Python library which used to plot the graphs. It is originally conceived by the John D. Hunter in 2002. The version was released in 2003, and the latest version is released 3.1.1 on 1 July 2019. It represents the data through the...
2 min read
In this tutorial, we will learn about the Python libraries for the PDF data extract for further analysis. We will go through the essential Python libraries. PDF is a portable document format which is generally used to store data safely. PDF resumes are created in various ways....
6 min read
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