Python: BubbleSort sorting algorithm


Tutorial on how to implement the BubbleSort sorting algorithm in Python 3 with animated demo and code implementation example.

► Insertion Sort
► Selection Sort
► Bubble Sort
► Merge Sort
► Quick Sort

► Code on GitHub
► Subscribe to my Channel
► Thank me on Patreon:


Xem Thêm Bài Viết Khác:


  1. Hey , but what is the "i" purpose here ? instead of for j in range(0, len(my_list) – 1 – i ) you could say just for j in range(i – 1) and then when you compare you could say if my_list[j] > my_list[i]: my_list[ j ], my_list[ i ] = my_list[ i ], my_list[ j ]. These gives me more hints about the function itself and have some logic , like j is the previous number and i is the next number to be compared to j.
    Does it make sense ?

  2. def bubbleSort(myList):
    for i in range (0, len(myList) – 1):
    for j in range(0, len(myList) – 1 – i):
    if myList[j] > myList[j+1]:
    myList[j], myList[j+1] = myList[j+1], myList[j]
    return myList

    theList = ['b', 'd', 'f', 'a', 'c', 'e']


  3. _sorted = False
    while not _sorted:
    _sorted = True

    for i in range(len(a)-1):
    if a[i] > a[i+1]:
    a[i], a[i + 1] = a[i + 1], a[i]
    _sorted = _sorted and False

    _sorted = _sorted and True

    I did it think way. Isn't it necessary to jump back to the top of the loop because we can't be certain the the minimum value will be placed at index 0?

  4. Hi,

    Can you explain how the range() function takes for arguments in line 5 at 1:47? I thought it could only take 3 and they had to be separated by a comma; like this: range(start, stop, step)

    Anyway well done, and very well explained


Please enter your comment!
Please enter your name here