Python Quick Sort

Source: Internet
Author: User

Quick Sort

The first one is very inefficient.

1 defQuickSort (array, Low, high):2     ifLow >=High :3         return4I, j =Low , high5TMP =Array[low]6      whileTrue:7          whileARRAY[J] > tmp andI <J:8J-= 19         ifI <J:TenArray[i] =Array[j] Onei + = 1 A          whileARRAY[J] < tmp andI <J: -i + = 1 -         ifI <J: theARRAY[J] =Array[i] -J-= 1 -         Else: -              Break +Array[i] =tmp -QuickSort (array, Low, i-1) +QuickSort (Array, j + 1, High)

The second way of writing, embodies the advantages of Python, very concise, fast

1 defQuickSort (L):2     ifLen (L) <= 1:3         returnL4Pivot =L[0]5lessor = QuickSort ([x forXinchL[1:]ifX <Pivot])6larger = QuickSort ([x forXinchL[1:]ifX >=Pivot])7     returnlessor + [Pivot] + larger

Of course, such a simple sort would be much less efficient than Python's built-in sorting algorithm.

Python Quick Sort

Related Article

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.