Last active
January 12, 2020 10:58
-
-
Save dovideh/f82310f0fe80fd35246b14d039055dc4 to your computer and use it in GitHub Desktop.
min swap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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