Python for quick sorting

Source: Internet
Author: User

Fast sorting is fast, probably thinking is: First use a numeric value as the median, by first sorting the array into two parts, the left is smaller than this value, the right is larger than this value, and then use recursion to the two parts of the array to the same sort:

1DefQuick_sort (Li, Start, end):2#Recursive end Condition:3If Start >=End4Return5#First Index on left6 left =Start7#The last index on the right8 right =End9#Take the first number as an intermediate valueTen mid =Li[left]11#First, the right-hand index moves to the left, and the loop is executed when the left<right is greater than the mid value.12While left <Right13While left < rightand Li[right] >=MidRight-= 1Li[left] =Li[right]16While left < rightand Li[left] <=Mid-Left + = 1Li[right] =Li[left]Li[left] =Mid20#At this point, the number to the left of mid is smaller than mid, and the number to the right of mid is greater than mid21 # The numbers on both sides are sorted recursively. Span style= "COLOR: #008080" >22 Quick_sort (Li, start, Left-123 Quick_sort (Li, left + 1, end) 24 25 26 if __name__ = = "  __main__ ' :27 li = [4, 3, 5, 7 , 9, 2, 1, 6, 8]28 Quick_sort (li, 0, Len (LI)-1) 29 print (LI)     

George Bernard Shaw said: "You have an apple, I have an apple, exchanged with each other, we still each have an apple; but you have a thought, I have a kind of thinking, exchange each other, only to find that from different angles to see the problem far more profound than itself, so much communication is a way to improve our self-ability!!!

If you have a better and simpler way, please be sure to comment thank you!!!

Python for quick sorting

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.