Python quick sorting algorithm instance analysis and python algorithm instance analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.