human
Give me a code to minimum number of swaps required to sort an array and the other steps required given the following:
Minimum number of swaps required to sort an array | swap function.
indexOf function.
Return the minimum number of swaps required to sort the array.
This is checking whether the current element is at the right place or not.
Swap the current element with the right index so that arr [ 0 ] to arr [ i ] is sorted.
Driver code.
Output will be 5"
How can the above be solved in Python?
assistant
#Here is code to minimum number of swaps required to sort an array and the other steps required.
 def swap ( arr , i , j ) :
	temp = arr [ i ]
	arr [ i ] = arr [ j ]
	arr [ j ] = temp
def indexOf ( arr , ele ) :
	for i in range ( len ( arr ) ) :
		if ( arr [ i ] == ele ) :
			return i
	return - 1
def minSwaps ( arr , N ) :
	ans = 0
	temp = arr . copy ( )
	temp . sort ( )
	for i in range ( N ) :
		if ( arr [ i ] != temp [ i ] ) :
			ans += 1
			swap ( arr , i , indexOf ( arr , temp [ i ] ) )
	return ans
if __name__ == " _ _ main _ _ " :
	a = [ 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 ]
	n = len ( a )
	print ( minSwaps ( a , n ) )