The most comprehensive and easy-to-understand algorithm - sorting algorithm [1]

1, bubble sorting

The basic idea of ​​bubble sorting is: starting from the unordered sequence head, making a pairwise comparison, The location is swapped according to size until the last largest (small) data element is swapped to the end of the out-of-order queue and becomes part of the ordered sequence; the next time the process continues until all data elements are in order.

# python [sorted sort]
s = [3,4,5,6,7,12,11]
n = len(s)

For i in range(len(s)-1):
    For j in range(len(s)-1-j):
Len(s)-1-j explained: The meaning of subtracting j here is that the largest number is already in the bottom and there is no need to compare.
        If s[j]>s[J+1]:
            Temp = s[j+1]
            s[j+1] = s[j]
            s[j] = temp
Print (s)
########Uploading and descending code, you can change the judgment symbol.
s = input().split()
n = len(s)
i =0
j =0

For i in range(n-1):
    For j in range(n-1):
        If s[n-j-2]<s[n-j-1]:
            Temp = s[n-j-2]

When the original sequence is "positive", the total number of comparisons of bubble sorting is n-1, and the number of moves is 0, which means that the time complexity of bubble sorting in the best case When O(n)

is sorted by the original sequence "reverse order", the total number of comparisons of bubble sorting is n(n-1)/2, and the number of moves is 3n(n-1)/2 times, so the bubble sort is at the most The time complexity in bad cases is O(n^2)

When the original sequence is disorderly, the average time complexity of bubble sorting is O(n^2)

2, quick sorting [CRAHoare was proposed in 1960]


解Idea: Decompose the original problem into several sub-problems that are smaller in size but similar in structure to the original problem. Recursively solve these sub-problems and then combine the solutions of these sub-problems into the solution to the original problem.

Principle: The data to be sorted is divided into two separate parts by scanning. All the data of one part is smaller than all the other parts of the data, and then the two parts of the data are sorted separately according to this method. The entire sorting process can be recursively, so that the entire data becomes an ordered sequence. [Official statement, thief sputum]

白话原理: For the disordered series, we take out one of the digits a, put the right side larger than a, put the left side smaller than a. In turn, the value b is our previous animation. The tree shape below is also well understood.


The above animation and tree diagram can show the principle of fast sorting. [Not enough obvious enough, enough vernacular]

## The simplest quick sorting solution
Def quicksort (a):
    If len(a) == 0 :
        Return []
        Pivot = a[0]
        Left = []
        Right = []
        Mid = []
        For x in a:
            If x < pivot:
            Elif x > pivot:
Note here that if there is no mid, recursion will report an error, python default recursion times 989, will fall into an infinite loop.
        Return quicksort(left) + mid +quicksort(right)
Nums = [6,1,2,7,9,3,4,5,10,8]
Print (quicksort(nums))

This is written according to the simplest thinking. The model of the program looks at the 白话解, vernacular explanation, vernacular explanation [important thing 3 times]