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 |
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