Advertisement
naren_paste

bubblesort

Feb 9th, 2024
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | Source Code | 0 0
  1. def bubblesortoptimised(arr):
  2.     for i in range(len(arr)-1, 0, -1):
  3.         issorted = True
  4.         for j in range(i):
  5.             if arr[j] > arr[j+1]:
  6.                 issorted = False
  7.                 arr[j], arr[j+1] = arr[j+1], arr[j]
  8.         if issorted:
  9.             print("The array is already sorted")
  10.             break
  11.  
  12. arr = [5, 6, 4, 3, 7, 3, 2, 8]
  13. bubblesortoptimised(arr)
  14. print(arr)
  15.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement