Python implements a quick-row

Source: Internet
Author: User

Quick Sort Intro
Fast sorting, also known as partition Exchange sorting, pick an element from the unordered queue, split the unordered queue into two separate parts, one part of all data is smaller than the other part of all the data, and then according to this method of the two parts of the data are quickly sorted, the entire sorting process can be recursive, To achieve the entire data into an ordered sequence.
Simply put: pick elements, divide groups, repeat the first two steps of grouping

Quick Sort Principle
By introducing the quick sort on the face, we know that the quick sort mainly includes the following two aspects:
Pick element Group, whole recursive grouping
Select elements to divide the group:

Characteristics:
1, because it is unordered queue, so the location can be randomly picked
2, temporarily divided into a space, storage we selected the intermediate elements
3. Left label position empty, move right label, vice versa
4, repeat 3, until the left and right side labels point to the same position,
5, the temporary storage of intermediate elements, the return to the position
Bottom line: A slow motion of the left hand, a slow-motion replay of the right hand

Overall Division:

Characteristics:
1. Recursive splitting
2, split to the end, the number of elements in all groups is 1
Bottom line: Recursive split to no more disassembly

Code Practice Analysis
According to the above two analysis, we should analyze from two major aspects:
Sequence cutting and recursive splitting

1. Sequence Cutting
Sequence cutting This knowledge point, we introduce from four aspects:
3 Basic labels, right push, left push, stop push (i.e. element homing)

1.1, 3 Basic labels
Size area cutting, involving at least three tags:
Mid: Specifies the temporary median number to be cut
Left: The label that is pushed from the side of the queue
Right: Labels pushed from the left side of the queue

1 defQuick_sort (Li, Start, end):2     #divide and conquer in Split3     #Start=end, proving that the data to be processed is only one4     #Start>end, prove no data on the right.5     ifStart >=End:6         return7     #defines two cursors, pointing to 0 and the end position, respectively8left =Start9right =EndTen     #put 0 position data, think it is the middle value OneMID =Li[left] A      whileLeft <Right : -         #let the right cursor move to the left to find a value less than mid and place it -          whileLeft < Right andLi[right] >=Mid: theRight-= 1 -Li[left] =Li[right] -         #let the left cursor move to the right to find a value greater than mid and place it in the cursor position -          whileLeft < Right andLi[left] <Mid: +Left + = 1 -Li[right] =Li[left] +     #when the while is finished, put mid in the middle position, Left=right ALi[left] =Mid at     #recursive processing of data on the left -Quick_sort (Li, Start, left-1) -     #recursive processing of data to the right -Quick_sort (Li, left+1, end) -   - if __name__=='__main__': inL = [6,5,4,3,2,1] -     #L = 3 [2,1,5,6,5,4] to     #[2, 1, 5, 6, 5, 4] +Quick_sort (L,0,len (L)-1) -     Print(L) the     #Stability: unstable *     #Optimal Time complexity: O (NLOGN) $     #Worst time complexity: O (n^2)

Key points:
Sequence Cutting:
1. Pick middle element: Mid = Alist[start]
2. Right push: While R > left and Alist[right] >= mid:
3. Left-forward: while Alist[left < right and "< mid:
4. Propulsion cycle: While left < right:
5. Element return: Alist[left] = mid
Recursive splitting:
1. Group Boundary Determination: Left = start, right = end
2. Recursive exit Condition: If start < end:
3. Function self-invocation: Quick_sort (alist, start, end)

Complexity of Time
Optimal time complexity: O (NLOGN)
For each quick row, the left and right labels are all moved around two volumes of data, which is the equivalent of traversing all the data, and the time complexity is O (n)
Because of the recursive grouping involved, his time complexity is O (LOGN)
Overall: The optimal time complexity is O (NLOGN)

Worst time complexity: O (n2)

Because the conditions of the recursive grouping grouping are not necessarily two points, it is possible that each mid specified is the largest or smallest, so how many elements can we divide into groups, the time complexity is O (n)
So the worst time complexity is O (N2), so the worst is still the case.
Stability: Unstable

Thinking:
Where to change, the result is descending?
While right > left and Alist[right] >= mid: >= in code changed to <=
While left < right and Alist[left] < in mid code < change to >
This section summarizes the following:
1. Quick sorting principle: pick elements, divide groups, repeat the first two steps in groups.
2. Quick sequencing Practice steps:
Dividing groups: Preparing for Work + left and right move + element homing
Recursive: function self-invocation + exit condition

Python implements a quick-row

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.