We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defcountSwaps(a):swaps=0forcheckina:forcounterinrange(len(a)-1):ifa[counter]>a[counter+1]:temp=a[counter+1]a[counter+1]=a[counter]a[counter]=tempswaps+=1returnprint("Array is sorted in %i swaps.\nFirst Element: %i\nLast Element: %i"%(swaps,a[0],a[-1]))
To build on this, I'm sure there's a library to swap easier or a different implementation of it.
Cookie support is required to access HackerRank
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 →
My Python3 solution:
To build on this, I'm sure there's a library to swap easier or a different implementation of it.