Hello friends, hope you are doing good. In this tutorial, I am going to share a python program to sort the element using bubble sort algorithm.
Bubble sort is the very simple sorting algorithm and it is a comparison-based algorithm in which each pair of adjacent elements compared and then elements are swapped if they are not in sorted in order. If you a college student or just started learning the Python programming language, then this program will help you more to the better understanding of bubble sort.
Below is the algorithms to sort the number using Bubble sort.
for all elements of the list
if list[i] > list[i+1]
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
if swapped == False:
arr = [45, 30, 25, 10, 5, 18, 70]
print ("Sorted array :")
for i in range(len(arr)):
print ("%d" %arr[i],end=" ")
5, 10, 18, 25, 30, 45, 70
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.