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('Enter list elements: ').split()
  12. a = [int(x) for x in a]
  13. bubble_sort(a)
  14. print('Sorted list: ', end='')
  15. print(a)

Output


Output!