human
Sort an array of 0 s , 1 s and 2 s | Utility function to print contents of an array.
Function to sort the array of 0 s , 1 s and 2 s.
Count the number of 0 s , 1 s and 2 s in the array.
Update the array.
Store all the 0 s in the beginning.
Then all the 1 s.
Finally all the 2 s.
Prthe sorted array.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to sort an array of 0 s , 1 s and 2 s and the other steps required.
 def printArr ( arr , n ) :
	for i in range ( n ) :
		print ( arr [ i ] , end = " ▁ " )
def sortArr ( arr , n ) :
	cnt0 = 0
	cnt1 = 0
	cnt2 = 0
	for i in range ( n ) :
		if arr [ i ] == 0 :
			cnt0 += 1
		elif arr [ i ] == 1 :
			cnt1 += 1
		elif arr [ i ] == 2 :
			cnt2 += 1
	i = 0
	while ( cnt0 > 0 ) :
		arr [ i ] = 0
		i += 1
		cnt0 -= 1
	while ( cnt1 > 0 ) :
		arr [ i ] = 1
		i += 1
		cnt1 -= 1
	while ( cnt2 > 0 ) :
		arr [ i ] = 2
		i += 1
		cnt2 -= 1
	printArr ( arr , n )
arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ]
n = len ( arr )
sortArr ( arr , n )