Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created October 29, 2019 13:22
Show Gist options
  • Select an option

  • Save imedadel/c12f5d257de344afebfac6724a41f9e3 to your computer and use it in GitHub Desktop.

Select an option

Save imedadel/c12f5d257de344afebfac6724a41f9e3 to your computer and use it in GitHub Desktop.
def countSwaps(a):
swaps = 0
n = len(a)
for i in range(n):
for j in range(n-1):
# Swap adjacent elements if they are in decreasing order
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j+1], a[j]
swaps += 1
print("Array is sorted in", swaps, "swaps.")
print("First Element:", a[0])
print("Last Element:", a[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment