DEV Community

Durga Pokharel
Durga Pokharel

Posted on

Day 16 of 100DaysofCode: A Simple Program To Do Bubble Sort

Today is my 16th day of #100Daysofcode. I learned more about python code to access web data including retrieving web page using urllib in python, parsing web page, web scraping on. Learned about BeautifulSoup and its important on coursera.

Learning sorting algorithms little at a time. First one is bubble sort. Attempt one challenge on Advent of Code 2020

Program To Do Bubble Sort

Bubble sort is a simple sorting algorithm that repeatedly step through the list, compare adjacent elements and swaps them if they are in a wrong order.

The pass through the list is repeated until the list is sorted. I tried to do one simple program on bubble sort which is given below.

# bubble sort
l1 = [2, 1, 4, 3, 5, 0]

def sort(l1):
    for i in range(len(l1)-1):
        for j in range(len(l1)-i-1):
            if l1[j]>l1[j+1]:
                l1[j], l1[j+1] = l1[j+1], l1[j]
    return l1

Enter fullscreen mode Exit fullscreen mode

Discussion (0)