Python: BubbleSort sorting algorithm

33
69



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

PYTHON SORTING ALGORITHMS
► Insertion Sort
► Selection Sort
► Bubble Sort
► Merge Sort
► Quick Sort

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

Nguồn:https://quydinh.com/

Xem Thêm Bài Viết Khác:https://quydinh.com/cong-nghe

33 COMMENTS

  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']

    print(bubbleSort(theList))

  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

    else:
    _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

LEAVE A REPLY

Please enter your comment!
Please enter your name here