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.