human
Sort n numbers in range from 0 to n ^ 2 | A function to do counting sort of arr [ ] according to the digit represented by exp ..
output array.
Store count of occurrences in count [ ].
Change count [ i ] so that count [ i ] now contains actual position of this digit in output [ ].
Build the output array.
Copy the output array to arr [ ] , so that arr [ ] now contains sorted numbers according to current digit.
The main function to that sorts arr [ ] of size n using Radix Sort.
Do counting sort for first digit in base n . Note that instead of passing digit number , exp ( n ^ 0 = 1 ) is passed ..
Do counting sort for second digit in base n . Note that instead of passing digit number , exp ( n ^ 1 = n ) is passed ..
Driver Code.
Since array size is 7 , elements should be from 0 to 48"
How can the above be solved in Python?
assistant
#Here is code to sort n numbers in range from 0 to n ^ 2 and the other steps required.
 def countSort ( arr , n , exp ) :
	output = [ 0 ] * n
	count = [ 0 ] * n
	for i in range ( n ) :
		count [ i ] = 0
	for i in range ( n ) :
		count [ ( arr [ i ] // exp ) % n ] += 1
	for i in range ( 1 , n ) :
		count [ i ] += count [ i - 1 ]
	for i in range ( n - 1 , - 1 , - 1 ) :
		output [ count [ ( arr [ i ] // exp ) % n ] - 1 ] = arr [ i ]
		count [ ( arr [ i ] // exp ) % n ] -= 1
	for i in range ( n ) :
		arr [ i ] = output [ i ]
def sort ( arr , n ) :
	countSort ( arr , n , 1 )
	countSort ( arr , n , n )
if __name__ == " _ _ main _ _ " :
	arr = [ 40 , 12 , 45 , 32 , 33 , 1 , 22 ]
	n = len ( arr )
	print ( " Given ▁ array ▁ is " )
	print ( * arr )
	sort ( arr , n )
	print ( " Sorted ▁ array ▁ is " )
	print ( * arr )