Knowledge in quick sorting: Key element selection and algorithm efficiency

Source: Internet
Author: User
Generally, the first or last element is used as the pivot element without full consideration. If the input is random, this is acceptable. However, if the input is pre-ordered or backward, such a pivot element produces a poor segmentation, because all elements are either input S1 or input S2. What's more, this happens in all recursive calls. Select the beginning and end elements as the hub element

Generally, the first or last element is used as the pivot element without full consideration. For the program example of selecting the first element as the pivot element, refer to the previous article "knowledge in quick sorting: Implementation of Hall quicksort" in the topic. for the program example that selects the last element as the pivot element, you can refer to the example in the introduction to fast sorting: Fast sorting process.

The sorting process of selecting the last element as the pivot element is as follows:

If the input is random, this is acceptable. However, if the input is pre-ordered or backward, such a pivot element produces a poor segmentation, because all elements are either input S1 or input S2. What's more, this happens in all recursive calls.

In fact, if the first element is used as the pivot element and the input is pre-ordered, the time consumed for fast sorting will be twice. However, pre-sorting input (or a large segment of pre-sorting data input) is also quite common. Therefore, the algorithm using the first element as the pivot element is not very efficient.

Another idea is to select the big ones in the first two different keys as the pivot element, but this is similar to selecting only the first element as the pivot element.

Randomly select hub element

A security policy is to randomly select the hub element.

In general, this policy is very safe unless there is a problem with the random number generator, because random hub elements cannot continuously produce inferior segmentation. On the other hand, the generation of random numbers is generally expensive, and the average running time of the rest of the algorithm cannot be reduced.

Division of three numbers

The median value of N in a group is the maximum value of [N/2. The best choice for pivot element is the value of the array.

However, this is difficult to calculate and will obviously slow down the speed of fast sorting. The estimation of such values can be obtained by randomly selecting three elements and using their values as the pivot element. In fact, randomness is not much helpful. Therefore, the average value of the three elements on the left, right, and center is used as the hub element. It is clear that the three-digit median segmentation method is used to eliminate the bad situation of pre-sorting input.

The efficiency of this kind of hub yuan selection is quite high.

There are also many variants of quick sorting. here we only study several representative algorithms.

Additional reading

The topic list of this article is as follows:

  1. Knowledge in quick sorting: start with guesses
  2. Knowledge in quick sorting: Let's look at the question of ball.
  3. Knowledge in quick sorting: Information entropy
  4. Knowledge in quick sorting: the process of quick sorting
  5. Knowledge in quick sorting: Hall and quick sorting
  6. Knowledge in quick sorting: Implementation of Hall's fast sorting
  7. Knowledge in quick sorting: Key element selection and algorithm efficiency
  8. Knowledge in quick sorting: randomization fast sorting

This article is available at http://www.nowamagic.net/librarys/veda/detail/2397.

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.