Algorithmic Analysis-Fast sequencing

Source: Internet
Author: User
Today, we say that the fast sorting algorithm, fast sequencing, and the idea of divide and conquer, can be considered as a classical algorithm, because the sorting efficiency is more efficient in the same O (N*logn) of several sorting methods, so it is often used.

In the average situation, sort N items to O (N*logn) comparison, and in the worst case, compare O (n2) comparisons.

In the best case, the datum for each partition is the median record of the current unordered area, with the result that the length of the two unordered sub-ranges of the datum are roughly equal. Total number of keyword comparisons: O (NLGN)

The worst case scenario is that each time the selected datum is the smallest (or largest) of the keywords in the current unordered region, the result is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and the number of records in another non-empty sub-range is divided by only one less than the number of records in the Time complexity O (n*n) The basic idea of the algorithm is to take a number from the sequence as the benchmark book first. The partitioning process will compare this large number of books to the right, with less than or equal to the book on the left. recursively (recursive) sorts sub-columns that are smaller than the base value elements and sub-columns that are larger than the base value elements. The detailed procedure is as follows

Sort the following series 9,3,6,7,4,5

Initially, i = 0; j = 5; X = a[0] = 9

The first step to take the first number of intervals is the base number (here is not necessarily the first, you can make any one) X = A (0) = 9, because
The number in a[0] has been saved to X, which can be understood as a hole dug in the array a[0].
The understanding array becomes as follows &,3,6,7,4,5

The second step is to find a smaller number than X, and when j = 5 satisfies the condition, put 5 in the pit. J – The array becomes 5,3,6,7,4,& in the order below.

The third step, from going after, looking for a number greater than X, no, this cycle is over. The array becomes 5,3,6,7,4,&. Can see the pits to the left of the tease than pits small. This way, the left side of the range continues to be sorted. This time choose 6 as Pit bit. 5,3,&,7,4,9

The fourth step adopts divide and conquer, and divides into each sub-interval according to the base. Recursive invocation
Repeat the steps. As follows. 5,3,4,7,&,9-"5,3,4,&,7,9-" 3,4,5,&,7,9

This recursively calls the individual pickup pieces, and finally sorts out the order of the 3,4,5,6,7,9.

//Quick sort-(void) Quicksork: (nsmutablearray*) List left: (Nsinteger) Left right: (Nsinteger) right{if (left < right) {
    Nsinteger key = [[list objectatindex:left] integervalue];
    Nsinteger low = left;

    Nsinteger = right;
        while (Low < high) {while ([[List Objectatindex:high] integervalue] > key) {high--;

        } [list Exchangeobjectatindex:low Withobjectatindex:high];
        while ([[List Objectatindex:low] integervalue] < key) {low++;
    } [list Exchangeobjectatindex:high withobjectatindex:low];
    } [List Replaceobjectatindex:low withobject:@ (key)];
    [Self quicksork:list left:left right:low-1];

    [Self quicksork:list left:low+1 right:right]; }
}       

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.