Python quick sorting algorithm instance analysis and python algorithm instance analysis
This example describes the Python quick sorting algorithm. We will share this with you for your reference. The details are as follows:
The time complexity of fast sorting is O (NlogN)
Algorithm Description:
① Extract a number from the sequence as the reference number
② In the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left
③ Repeat the second step for the left and right intervals until each interval has only one number.
Suppose that we sort 6, 1, 2, 7, 9, 3, 4, 5, 10, 8. First, we can find a reference number in this sequence (for reference ), for example, if "6" is selected as the benchmark number, place all the numbers greater than the benchmark number on the right of "6" and the numbers smaller than "6" on the left.
Principle Analysis:
① Select the leftmost number as the base number key.
② Set two cursors, low and high, pointing to the lowest and highest bits of the array respectively.
③ Then, the system moves forward first. If the number in the high bit is greater than the key, the system moves forward. If the number in the high-1 bit is greater than the key, the system continues to move forward, until the number on this bit <= key
④ At this time, it is relatively low bit. If <= key, low goes backward and becomes low + 1, and so on until the number on this bit is greater than the key
⑤ Switch the number of high and low bits
6. Repeat the preceding steps until low = high and exchange the values of key and high bits.
7. Perform recursive operations
Sample Code:
#! /Usr/bin/env python # coding: UTF-8 # Set the delimiter and maximum def quickSort (nums, low, high ): # set a benchmark key = nums [low] while low
Running result:
PS: For more information about sorting algorithms, see online tools on this site:
Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process Tool
Http://tools.jb51.net/aideddesign/paixu_ys