Lesson 1: Sorting Algorithms - English
Bubble Sort
The bubble sort is a sorting algorithm. Two elements of the set are compared. If the second item isn't the largest, the elements are switched. Then, the next elements are compared. When all the elements are compared, the largest are moved to the end of the set. This has its advantages and disadvantages. The Bubble sort's main advantage is that it is easy to implement. However, this algorithm is very slow. For larger sets, there are better algorithms. Its worst case and average time complexity is O(n2). The best case time complexity is O(n), which are bad cases.
Example Code
def main():
list = [6,5,37,2,7,3,6,42,67,3,17,25]
sorted = False
while not sorted:
sorted = True
for item in range(0, len(list) - 1):
if list[item] > list[item+1]:
sorted = False
list[item], list[item+1] = list[item+1], list[item]
main()