"Introduction to Algorithms" Sort (iii): Rapid sequencing In-depth analysis

Source: Internet
Author: User

Five, quick sort

Fast sorting is one of the most common sort algorithms, including C qsort,c++ and Java sort, with fast rows (c + + and Java sort optimized and mixed with other sorting algorithms).

Fast row worst case O (n^2), but the average efficiency o (n lg N), and this o (n lg N) Number of the implied constant factor is very small, fast row can be said to be the fastest sorting algorithm, not the wave of the title. It is also sorted in place.

A quick sort is based on a divide-and-conquer model:

Decomposition: Array A "p.. R "is divided into two (possibly empty) sub arrays a" p. Q-1 "and a" Q+1..R ", so that a" p. Each element in Q-1 is less than or equal to a (q) and is less than or equal to the element in a "Q+1..R". Subscript Q is also 迕 in the return partitioning process.

Workaround: Quick Sort by recursive invocation, sub array a "P". Q-1 "and a" q+1..r "sort.

Merging: Because two sub arrays are sorted in place, merging them does not require an action: the entire array a "P". R "is sorted.

The following procedure implements a quick sort:

Array partitioning: Partition (key, it's sub-array a "p. R "for in-place rearrangement)

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.