April 14, 2024, 5:52 p.m. | Main

DEV Community dev.to


#Optimized bubble sort
def bubble_sort(lst):

swapped = True
c = 0
while swapped == True:
swapped = False
for j in range(len(lst)-1):

if(lst[j]>lst[j+1]):
lst[j], lst[j + 1] = lst[j + 1], lst[j] #swap the elements
swapped = True
c +=1
return c

#sort a list
L = [9, 0, 2, 1, 0, 1, 100, -2, 8, 7, 4, 3, 2]

bubble_sort(L)
print("The sorted list is: ", L)

Bubble sort is the most basic sorting algorithm. Its simplicity makes it suitable …

bubble false list python true

Data Engineer

@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania

Artificial Intelligence – Bioinformatic Expert

@ University of Texas Medical Branch | Galveston, TX

Lead Developer (AI)

@ Cere Network | San Francisco, US

Research Engineer

@ Allora Labs | Remote

Ecosystem Manager

@ Allora Labs | Remote

Founding AI Engineer, Agents

@ Occam AI | New York