Data Structure-fast Sorting Algorithm

Source: Internet
Author: User

Data Structure-fast Sorting Algorithm

Let's talk about quick sorting today:

Basic Idea:

Any element (such as the first one) that is smaller than the axis is put before, and all elements larger than the axis are put, create two sub-tables to re-select the central element for each sub-table and adjust it according to this rule until each sub-table has only one element

Note:

The subtable of each trip is formed by an approximation from the two ends to the intermediate order. Because each trip has similar operations on each subtable, a recursive algorithm can be used.

Code implementation:

# Include
  
   
Using namespace std; // locate the first axis point. Use this axis point as the border and divide the array to be sorted into // [low, slow), [Slow], (slow, high] int Partition (int a [], int low, int high) {if (low> high) // judge the return-1 condition; int cursor = a [low]; // set the axis point to the first element a [low] of the array by default (Backup) while (low 

Output result:

Time Efficiency:O (nlog2n)-the number of elements determined by each trip increases exponentially.

Space efficiency:O (log2n)-stack space is required for Recursion

Stability:Unstable-any element is the pivot.

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.