Preview lessons, content and tests

Computer Science & Programming solved. All in one platform.

1. To trial the platform and take tests, please take a few seconds to SIGN UP and SET UP FREE.
2. Searching for something specific? See our text overview of all tests. Includes 'real teacher' use videos.

Join 30000+ teachers and students using TTIO.

Bubble Sort

Bubble sort is a comparison?-based algorithm that compares each pair of elements in an array and swaps them if they are out of order until the entire array is sorted.

Suggested Video


#1 Write a Python program to sort a list of elements using the bubble sort algorithm

#2 Comment each line of the python solution below to show your understanding of the algorithm

Try it yourself


def bubbleSort(nlist):
    for passnum in range(len(nlist)-1,0,-1):
        for i in range(passnum):
            if nlist[i]>nlist[i+1]:
                temp = nlist[i]
                nlist[i] = nlist[i+1]
                nlist[i+1] = temp

nlist = [14,46,43,27,57,41,45,21,70]

Animated Demo


Flowchart: Python Data Structures and Algorithms: Bubble sort