Sorting Algorithm (2): Detailed Explanation of Selection 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: The selection sort is actually quite easy to remember. It is easy to associate by looking at the name of the sort. "Selection" is the core idea of ​​this sort, so how do you choose when you choose? In fact, if you remember this sort, you should associate with the best Or the elimination mechanism is easy to remember: the best choice is to choose the best from a bunch of uneven items of the same kind, such as a pair of apples, big and small, we choose the big ones and eat; elimination is to choose the best from a bunch of uneven ones. The most rotten ones are eliminated. For example, there are still a bunch of apples. This time, they are apples with different degrees of rot or are about to rot. We need to choose the most rotten apples and discard them.

Logic implementation: Is the above memory method very close to life, so that it is easy to remember. The logic is actually very simple, choose one of the best or the worst at a time, and put them in an ordered queue in turn.

Code implementation:
forward sorting:

def select_sort(my_list):
    n = len(my_list)
    for j in range(n-1):
        min_index = j
        for i in range(j+1, n):
            if my_list[min_index] < my_list[i]:
                min_index = i
        my_list[j], my_list[min_index] = my_list[min_index], my_list[j]
        print("第%s趟:" % (j + 1), my_list)


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


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

Reverse sort:

def select_sort(my_list):
    n = len(my_list)
    for j in range(n-1):
        min_index = j
        for i in range(j+1, n):
            if my_list[min_index] > my_list[i]:
                min_index = i
        my_list[j], my_list[min_index] = my_list[min_index], my_list[j]
        print("第%s趟:" % (j + 1), my_list)


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

Summary of selection sorting:
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 in the application scenario.

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 selection 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 sorting does not apply for storage space in the process, so we think that the space complexity of selection sorting 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/116137808