Python Program to Sort an array of 0s, 1s and 2s Last Updated : 11 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last. Examples: Input : {0, 1, 2, 0, 1, 2, 2, 2, 2, 1} Output : {0, 0, 1, 1, 1, 2, 2, 2, 2, 2} Input : {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output : {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2} Simple Solution : We create three empty lists. We add all 0s to first list, 1s to second list and 2s to third list. Finally we append all lists and return Python 1== # Function for sort def SortWithoutSorting(arr): # 3 Empty list for initialize 0 1 and 2 l1 =[] l2 =[] l3 =[] for i in range(len(arr)): if arr[i] == 0: l1.append(arr[i]) elif arr[i] == 1: l2.append(arr[i]) else: l3.append(arr[i]) return (l1 + l2 + l3) # Driver Code arr = array.array('i', [0, 1, 0, 1, 2, 2, 0, 1]) print(SortWithoutSorting(arr)) Efficient Solution The above solution requires extra space. How to do it without extra space (in-place) in the same given list and using only one traversal. Please refer Sort an array of 0s, 1s and 2s for implementation of the efficient solution. Comment More infoAdvertise with us Next Article Python | Sort Tuples in Increasing Order by any key _knking_ Follow Improve Article Tags : Python Python-sort Practice Tags : python Similar Reads Segregate 0's and 1's in an array list - Python Segregating 0s and 1s in an array involves rearranging the elements so that all 0s appear before 1s. This can be done using different methods, such as list comprehension, sort() method or a two-pointer approach, each offering varying levels of efficiency and simplicityUsing List ComprehensionTo segr 3 min read Python Program for Bitonic Sort Bitonic Sequence: A sequence is called Bitonic if it is first increasing, then decreasing. In other words, an array arr[0..n-i] is Bitonic if there exists an index i where 0<=i<=n-1 such that x0 <= x1 â¦..<= xi and xi >= xi+1â¦.. >= xn-1 A sequence, sorted in increasing order is cons 4 min read Python program to Sort a list according to the second element of sublist Sorting a list by the second element of each sublist means rearranging the sublists based on the value at index 1. For example, given the list [[1, 3], [2, 1], [4, 2], [3, 5]], sorting by the second element results in [[2, 1], [4, 2], [1, 3], [3, 5]], where the second elements (1, 2, 3, 5) are in as 3 min read Python | Sort Tuples in Increasing Order by any key Given a tuple, sort the list of tuples in increasing order by any key in tuple. Examples: Input : tuple = [(2, 5), (1, 2), (4, 4), (2, 3)] m = 0 Output : [(1, 2), (2, 3), (2, 5), (4, 4)] Explanation: Sorted using the 0th index key. Input : [(23, 45, 20), (25, 44, 39), (89, 40, 23)] m = 2 Output : So 3 min read NumPy Array Sorting | How to sort NumPy Array Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.ar 4 min read Sort a list of strings in lexicographical order in Python We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:Input: ["banana", "apple", "cherry", "dat 2 min read Like