human
Sort an array of 0 s , 1 s , 2 s and 3 s | Function to sort the array having array element only 0 , 1 , 2 , and 3.
Iterate until mid <= j.
If arr [ mid ] is 0.
Swap integers at indices i and mid.
Increment i.
Increment mid.
Otherwise if the value of arr [ mid ] is 3.
Swap arr [ mid ] and arr [ j ].
Decrement j.
Otherwise if the value of arr [ mid ] is either 1 or 2.
Increment the value of mid.
Iterate until i <= j.
If arr [ i ] the value of is 2.
Swap arr [ i ] and arr [ j ].
Decrement j.
Otherwise , increment i.
Print the sorted array arr [ ].
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to sort an array of 0 s , 1 s , 2 s and 3 s and the other steps required.
 def sortArray ( arr , N ) :
	i = 0
	j = N - 1
	mid = 0
	while ( mid <= j ) :
		if ( arr [ mid ] == 0 ) :
			arr [ i ] , arr [ mid ] = arr [ mid ] , arr [ i ]
			i += 1
			mid += 1
		elif ( arr [ mid ] == 3 ) :
			arr [ mid ] , arr [ j ] = arr [ j ] , arr [ mid ]
			j -= 1
		elif ( arr [ mid ] == 1 or arr [ mid ] == 2 ) :
			mid += 1
	while ( i <= j ) :
		if ( arr [ i ] == 2 ) :
			arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
			j -= 1
		else :
			i += 1
	for i in range ( N ) :
		print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ]
	N = len ( arr )
	sortArray ( arr , N )