Created
October 29, 2019 13:22
-
-
Save imedadel/c12f5d257de344afebfac6724a41f9e3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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