Fast sequencing of Python algorithms

Source: Internet
Author: User

Algorithmic Thinking: Quick sort uses the idea of divide and conquer, that is, select a datum in the selected array (either one can), base on the base of the reference, move the elements less than that datum to the left of the datum, move the data that is larger than the datum to the right, and then recursively process the left and right sides.                                                            Also according to the above method, that is: Select the benchmark, in recursion. Algorithm example: arr=[10,5,2,3,4,7,6]---> [2, 3, 4, 5, 6, 7, 10] The code is as follows:

          

Several of the above code can be simplified as follows:

Greater,less = [],[]

For i in Arr[1:]: Lite version less = [I-I in arr[1:] if I <= pivot]

If I <= pivot: ==========>>>>> greater = [I for I in arr[1:] if i > Pivot]

Less.append (i)

Else

Greater.append (i)

Algorithm Performance Analysis:

In the worst case, the time complexity is O (n^2)

On average, the time complexity is O (NLOGN)

There are two key points in the quick sort:

First, the judgment of the recursive exit

For recursive exits, we can consider several special cases:

When there are no elements in the array, it should be returned directly, and should be returned directly when there is only one element in the array. Therefore, when an element in an array is empty or has only one element, the program should return.

Second, the determination of recursive expressions

For a quick sort, consider it from its algorithmic thinking, which should be the case.

[Less than Datum] + [datum] + [greater than Datum]

After we have made this clear, we then call the quick sort on the less-than-datum and larger-than-datum parts. This allows us to get a fast-line recursive expression.

Quicksort (Less_part) + [benchmark] + quicksort (great_part)

    

The middle process of fast sequencing is actually a recursive tree. When the recursion reaches the leaf node, then the recursion ends, and the program executes.

Fast sequencing of Python algorithms

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.