You are viewing a single comment's thread. Return to all comments →
Below worked for me.. this took much time...
def minimumSwaps(arr): swaps = 0 #mid = 0 while (listSorted(arr) == False): for i in range(0, len(arr)): if arr[i] != i+1: j = arr[i]-1 if i < j and arr[i] > arr[j]: arr[i], arr[j] = arr[j], arr[i] swaps += 1 elif i > j and arr[i] < arr[j]: arr[i], arr[j] = arr[j], arr[i] swaps += 1 return swaps def listSorted(arr): for i in range(0, len(arr)): if arr[i] != i+1: return False
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
Below worked for me.. this took much time...