Python data structure ----- recursion to achieve quick sort

Table of contents

Foreword:

quick sort

1. Concept

 2. Example

Python code implementation

Recursive quick sort


Foreword:

         Today we will learn the thinking method of quick sort together, and then use the Python language to realize the function of quick sort, let's start today's study!

quick sort

1. Concept

        Quick sort is actually an improved form of bubble sort. Its sorting speed is fast and the time complexity is small. It has become a sorting method encapsulated in many programming languages.

implement logic

Quicksort uses a divide and conquer strategy to divide a sequence (list) into two sub-sequences (sub-lists).

① Pick an element from the array, called "pivot",
② Reorder the array, all elements smaller than the pivot value are placed in front of the pivot, and all elements larger than the pivot value are placed behind the pivot (the same number can go to either side). After this partition exits, the benchmark is in the middle of the sequence. This is called a partition operation.
③ Recursively sort the sub-sequences of elements smaller than the reference value and the sub-sequences of elements greater than the reference value.

When recursing to the bottom, the size of the array is zero or one, that is, it has been sorted. This algorithm will definitely end, because in each iteration (iteration), it will put at least one element in its last position.

 The diagram is as follows:

Four Optimizations for Quick Sort

 2. Example

Here is a set of numbers, 3,5,7,1,3,6 as shown below, to sort this set of numbers

 The first step is to select the benchmark number temp, usually the first number is used as the benchmark number, and then mark the positions of i and j, which are the head and the tail respectively.

 The second step is to start moving j and i under the premise of i<j. The principle is to move j first, then move i. When j encounters a number smaller than the number pointed to by i, it stops moving, and then starts moving i. When When i encounters a number larger than temp, it stops moving. At this time, it starts to exchange the numbers pointed to by i and j, and so on.

In the third step , when i and j move to the meeting position, exchange the number pointed to by i and j with temp. At this time, you will find that the right side of the exchanged number is larger than this number, and the left side is larger than this number. is smaller than this number.

 The fourth part is to enter recursion. At this time, we need to recurse on the left and right sides of the number pointed to by the exchange of i and j in the previous step, that is, the two sides go through the entire previous process again, and the final result is the number that has been sorted up.

Python code implementation

Recursive quick sort

#快速排序
import random as r

def quick(left,right,li):
    if left>right:
        return
    temp=li[left]#获取到数组第一个数字
    i=left #标记i 的位置
    j=right #标记 j的位置
    while i!=j:
        while li[j]>=temp and i<j:
            j-=1
        while li[i]<=temp and i<j:
            i+=1
        if i<j:
            li[i],li[j]=li[j],li[i] #数字交换
    li[left],li[i]=li[i],li[left] #当i和j相遇的时候,进行与temp数字交换

    quick(left,i-1,li) #左边进入递归
    quick(i+1,right,li) #右边进入递归
    return

if __name__ == '__main__':
    li=[r.randint(0,20) for _ in range(10)]
    print(li)
    quick(0,len(li)-1,li)
    print(li)
#输出结果:
#[2, 11, 20, 5, 13, 16, 11, 14, 20, 8]
#[2, 5, 8, 11, 11, 13, 14, 16, 20, 20]

 Well, that's all for today, see you in the next issue!

Share a wallpaper:

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/130268352