human
Minimum number of swaps required to sort an array | Return the minimum number of swaps required to sort the array.
Dictionary which stores the indexes of the input array.
This is checking whether the current element is at the right place or not.
If not , swap this element with the index of the element which should come here.
Update the indexes in the hashmap accordingly.
Driver code.
Output will be 5"
Solution in Python:
 ... Can you provide a code to minimum number of swaps required to sort an array and the other steps required?
assistant
#Here is code to minimum number of swaps required to sort an array and the other steps required.
 def minSwap ( arr , n ) :
	ans = 0
	temp = arr . copy ( )
	h = { }
	temp . sort ( )
	for i in range ( n ) :
		h [ arr [ i ] ] = i
	init = 0
	for i in range ( n ) :
		if ( arr [ i ] != temp [ i ] ) :
			ans += 1
			init = arr [ i ]
			arr [ i ] , arr [ h [ temp [ i ] ] ] = arr [ h [ temp [ i ] ] ] , arr [ i ]
			h [ init ] = h [ temp [ i ] ]
			h [ temp [ i ] ] = i
	return ans
a = [ 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 ]
n = len ( a )
print ( minSwap ( a , n ) )