Skip to content

Instantly share code, notes, and snippets.

@ovidiucs
Last active January 12, 2020 10:58
Show Gist options
  • Save ovidiucs/f82310f0fe80fd35246b14d039055dc4 to your computer and use it in GitHub Desktop.
Save ovidiucs/f82310f0fe80fd35246b14d039055dc4 to your computer and use it in GitHub Desktop.
min swap
def minimumSwaps(arr):
swp = 0
n = len(arr)
print(arr)
for i in range(1,n+1):
for j in range(n):
if(arr[arr.index(i)] == arr[j]):
source_idx = arr.index(i)
destination_idx = i-1
if source_idx != destination_idx:
arr[destination_idx],arr[source_idx] = arr[source_idx],arr[destination_idx]
swp += 1
break
return swp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment