Sorting Algorithm (1): Detailed Bubble Sort

Foreword: I have recently changed jobs, and interviews with a slightly larger factory will be asked about the knowledge of algorithms and data structures. I have time to summarize it myself. If the summary is not in place, I hope the boss will correct me.

Episodic memory: In fact, Bubble Sort is rotten in school, but it is very strange why sometimes you forget to implement the logic after a while? It is not used often. To be honest, it is generally not used in work, but we can use scene memory to engrave it in our minds: you only need some common sense in life to remember it very firmly, and compare it to fish spit In the bubble scene, the bubbles that the fish spit under the water are very small when they just spit out. As the bubbles rise, the bubbles will become larger and larger, and eventually surface and break. This scene is very similar to ours. The bubble sort thinks of sorting from small to large, and the process of breaking a bubble is regarded as a sort of trip.

Logic implementation: After talking about the memory method, the logic is actually very clear. Compare adjacent values ​​in turn, and move the largest or smallest to the end.

Code implementation:
forward sorting:

def bubble_sort(list):
    list_len = len(list)
    for i in range(0, list_len):
        print("第%d趟" % (i+1))
        for j in range(1, list_len):
            if list[j-1] < list[j]:
                list[j-1], list[j] = list[j], list[j-1]
        print(list)


if __name__ == '__main__':
    my_list = [7, 2, 0, 1, 5, 6, 8, 3, 9, 4]
    bubble_sort(my_list)


运行结果:
第1[2, 0, 1, 5, 6, 7, 3, 8, 4, 9]2[0, 1, 2, 5, 6, 3, 7, 4, 8, 9]3[0, 1, 2, 5, 3, 6, 4, 7, 8, 9]4[0, 1, 2, 3, 5, 4, 6, 7, 8, 9]5[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]6[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]7[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]8[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]9[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]10[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Reverse sort:

def bubble_sort(list):
    list_len = len(list)
    for i in range(0, list_len):
        print("第%d趟" % (i+1))
        for j in range(1, list_len):
            if list[j-1] < list[j]:
                list[j-1], list[j] = list[j], list[j-1]
        print(list)


if __name__ == '__main__':
    my_list = [7, 2, 0, 1, 5, 6, 8, 3, 9, 4]
    bubble_sort(my_list)
 
 
运行结果:
第1[7, 2, 1, 5, 6, 8, 3, 9, 4, 0]2[7, 2, 5, 6, 8, 3, 9, 4, 1, 0]3[7, 5, 6, 8, 3, 9, 4, 2, 1, 0]4[7, 6, 8, 5, 9, 4, 3, 2, 1, 0]5[7, 8, 6, 9, 5, 4, 3, 2, 1, 0]6[8, 7, 9, 6, 5, 4, 3, 2, 1, 0]7[8, 9, 7, 6, 5, 4, 3, 2, 1, 0]8[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]9[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]10[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Summary of Bubble Sort:
There is no best algorithm, only the most suitable algorithm. The quality of an algorithm is usually related to the time complexity, space complexity and stability of the algorithm.

Time complexity: The measure of time complexity is the synthesis of the time it takes to compare and exchange positions each time, that is, the sum of the time it takes to complete the sorting is called time complexity; the calculation standard is one cycle (no matter how many times) we Denoted as n, if we can denote the difference into a binary tree structure as log n, then the time complexity of bubble sorting can be seen to be O(n^2)

Space complexity: The measure of space complexity is whether there is a new storage space applied for to complete the sorting during the whole process of sorting. If so, and the more space is applied, then we consider the space complexity of the algorithm to be about complex. , then bubble sort does not apply for storage space in the process, so we think that the space complexity of bubble sort is small (except for itself)

Stability: that is, in the original sequence, list[i]=list[j], and list[i] is before list[j], and in the sorted sequence, list[i] is still before list[j], Then this sorting algorithm is called stable, otherwise it is called unstable, then we think that bubble sorting is stable, stability comparison: heap sorting, quick sorting, Hill sorting, selection sorting are unstable sorting algorithms, Bubble sort, insertion sort, and merge sort are stable sorting algorithms.

Guess you like

Origin blog.csdn.net/xiaoxin_OK/article/details/115772540