You are viewing a single comment's thread. Return to all comments →
Python 3 solution:
n = int(input().strip()) a = list(map(int, input().strip().split(' '))) numSwaps = 0 while True: SwapsFlag = False for i in range(len(a)-1): if a[i] > a[i+1]: a[i], a[i+1] = a[i+1], a[i] numSwaps += 1 SwapsFlag = True if not SwapsFlag: break print('Array is sorted in', numSwaps, 'swaps.') print('First Element:', a[0]) print('Last Element:', a[-1])
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting: Bubble Sort
You are viewing a single comment's thread. Return to all comments →
Python 3 solution: