Python Program to Implement Bubble Sort



Bubble Sort is a sorting algorithm where we repeatedly iterate through the array and swap adjacent elements that are unordered.



  1. #Python program to implement Bubble sort
  2. def bubble_sort(a):
  3. for i in range(len(a) - 1, 0, -1):
  4. temp = True
  5. for j in range(0, i):
  6. if a[j + 1] < a[j]:
  7. a[j], a[j + 1] = a[j + 1], a[j]
  8. temp = False
  9. if temp:
  10. return
  11. a = input(&#x27;Enter list elements: &#x27;).split()
  12. a = [int(x) for x in a]
  13. bubble_sort(a)
  14. print(&#x27;Sorted list: &#x27;, end=&#x27;&#x27;)
  15. print(a)

Output
Output!




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube