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.
if__name__=='__main__':n=int(input().strip())a=list(map(int,input().rstrip().split()))# Write your code hereswaps=0last_idx=len(a)-1whileTrue:no_swaps=Trueforiinrange(last_idx):ifa[i]>a[i+1]:buf_val=a[i+1]a[i+1]=a[i]a[i]=buf_valswaps+=1no_swaps=False# If no swaps occured, the array must be sorted; break outifno_swaps:breakprint(f"Array is sorted in {swaps} swaps.")print(f"First Element: {a[0]}")print(f"Last Element: {a[last_idx]}")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 20: Sorting
You are viewing a single comment's thread. Return to all comments →