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.
current_num=0swap_num=0# Make a number of checks and potential changes equal to the length of the# array.foriinrange(len(a)):forjinrange(len(a)-1):ifa[j]>a[j+1]:# Store the current number before it changes.current_num=a[j]# Perform the swap.a[j]=a[j+1]a[j+1]=current_num# Count up 1 swap.swap_num+=1# Print out the statements.print(f"Array is sorted in {swap_num} swaps.")print(f"First Element: {a[0]}")print(f"Last Element: {a[len(a) - 1]}")return
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 →
Python Solution