Bubble Sort is a internal sorting method by which a set unsorted of elements can be sorted in ascending or descending order very easily. Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average.
# Program for bubble sort
# You can take list from user or assign a list.
list=[5,1,4,2,8]
flag = 1
pos = 0
l = len(list)
count = 0
while pos <= l-1 and flag ==1 :
flag = 0
for j in range(0,(l-1-pos)) :
if list[j] > list[j+1] :
list[j],list[j+1] = list[j+1],list[j]
flag = 1
count += 1
print("The shorted list is:",list)
# This will print the number of pass.
print("The number of pass:",count)
Comments