Fast-forward Variant

Source: Internet
Author: User

The worst case of randomization fast sorting is based on the choice of the principal component for each division. Select the first element as the principal element for basic quick sorting. In this way, when the array is ordered, the worst result is obtained for each division. A common optimization method is the randomization algorithm, that is, randomly selects an element as the principal component. In this case, although the worst case is O (n ^ 2), the worst case is no longer dependent on input data, but the random function value is not good. In fact, the probability of a rapid randomization sorting to obtain the theoretical worst case is only 1/(2 ^ N ). Therefore, the random fast sorting can be performed on the time complexity of the vast majority of input data reaching O (nlogn. A predecessor made an incisive conclusion: "randomization and fast sorting can satisfy a person's character needs for a lifetime ." The only drawback of rapid randomization sorting is that, once many identical data are input, the Randomization effect will be reduced directly. For the limit condition, that is, for N identical number sorting, the time complexity of the Randomization fast sorting will undoubtedly be reduced to O (N ^ 2 ). The solution is to use a method for scanning so that the principal component is retained in the original position without switching. Balanced quicksort tries its best to select an element that can represent the value as the key data each time, and then compares, replaces, and Recursion according to the general principle of quick sorting. Generally, the method for selecting this data is to take the start, end, and center data and select the value through comparison. The advantage of getting these three values is that, in actual problems, there is a high probability of an approximate sequence data or a reverse sequence data. At this time, the intermediate data must be the mean value, which is also the actual approximate mean value. In case of data that happens to be big on both sides (or vice versa) in the middle, the retrieved values are close to the nearest value, because at least two parts can be separated, the actual efficiency will increase by about two times, it is also conducive to slightly disrupting data and damaging the degraded structure. The difference between the external quicksort and the ordinary quick sort is that the key data is a buffer, first, read the previous and subsequent M/2 elements into the buffer and sort these elements in the buffer, and then read the next element from the beginning (or end) of the sorted array, if this element is smaller than the smallest element in the buffer, it is written to the space at the beginning; if this element is greater than the largest element in the buffer, it is written to the final space; otherwise, the maximum or minimum element in the buffer is written into the array and the element is put in the buffer. Keep the maximum value lower than the key data, and the minimum value is higher than the key data, so as to avoid re-arrangement of the intermediate data. After completion, the middle space of the array must be empty, and the buffer is written into the middle space of the array. Then recursively sorts the smaller external parts cyclically. Three-way quick baseline ( Three-way Radix quicksort, also known as m Ultikey quicksort, multi-key quicksort) : Combined with the characteristics of base sorting (Radix sort, for example, the general string sorting is base sorting) and fast sorting, it is a more efficient algorithm in string sorting. This algorithm is used to sort the elements of an array, that is, multikey, such as a string. Each letter can be considered as a key. The algorithm selects an element as the key data in the sorted array at any time. First, only the first key (letter) of the element is considered ), then, the comparison of other elements by key is divided into three parts: less than, equal to, and greater than key data. Then, recursively sort the "less than" and "greater than" parts based on this key location, and sort the "equal to" parts based on the next key.

Fast-forward Variant

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.