Skip to content

Instantly share code, notes, and snippets.

@cmanny
Last active April 11, 2017 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmanny/7a14485aabb37df8d4dc0ac97562e712 to your computer and use it in GitHub Desktop.
Save cmanny/7a14485aabb37df8d4dc0ac97562e712 to your computer and use it in GitHub Desktop.
def count_swaps(nums):
swaps = 0
nn = [x for x in nums]
l = len(nn)
for i, j in zip(range(l - 2, -1, -1), range(l - 1, 0, -1)) * l:
if nn[i] > nn[j]:
nn[j], nn[i] = nn[i], nn[j]
swaps += 1
return swaps
print(count_swaps([1,2,3,4,5,6]))
print(count_swaps([1,5,3,4,6,2]))
print(count_swaps([6,5,4,3,2,1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment