"Python Learning notes-data structures and algorithms" quick sort

Source: Internet
Author: User

"Quick Sort":

Using recursive algorithm, first select a Datum value (pivot value), here we select the first value of the list as an example. The purpose of this benchmark is to assist in the segmentation of the list.

This datum is in the correct position in the positive sequence table, which we call the split point. This point is used to divide the list into two parts, and then make a quick sort of each part.

The segmentation process is as follows:

First we select the first element of the list as the base value, and this example is 54.

  

Next, look for the split point, in which the elements less than the base value are moved to the left of the split point, and the elements that are larger than the base value are moved to the right of the split point.

To do this, we set two markers Leftmark and RightMark to track the process of scanning individual elements:

When an element of the Leftmark position is less than or equal to the base value, Leftmark moves to the right one position to continue scanning, and the scan stops when the element of the Leftmark position is greater than the base value.

Similarly, when an element of the RightMark position is greater than or equal to the base value, RightMark moves to the left one position to continue scanning, and the scan stops when the element of the RightMark position is less than the base value.

After stopping the scan, we compare the size of the Leftmark and RightMark, if the Rightmark<leftmark,rightmark is the position where the datum should be stored, that is, the split point, we exchange the reference value to this position, Returns the split point position for the next recursive operation; otherwise swaps the elements of two positions.

  

After we have put the datum values in the correct position, we see that the elements on the left side of the base value are smaller than the datum values in the current list, and the elements on the right are larger than the datum values. So we divide the list into two sub-lists with the base value as the split point, and then continue the sub-list for quick sorting until the child list is 0 or 1.

  

"Implementation of quick Sort"

  

"Performance Analysis"

Fast sorting does not require extra space compared to merge sort, but the disadvantage is that the list may not be split evenly and will result in less efficiency.

The worst case scenario is that each partition divides the list into a 0-length sub-list and a sub-list of length n-1, in which case the time complexity is O (N2).

We can reduce this kind of non-average segmentation by changing the selection method of the reference value, one method is to select the first element of the list, the middle element, and the value in the middle of the last element as the base value.

"Python Learning notes-data structures and algorithms" quick sort

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.