Fast sorting (1) Basic Algorithm Implementation and Sorting Algorithm Implementation

Source: Internet
Author: User

Fast sorting (1) Basic Algorithm Implementation and Sorting Algorithm Implementation

    • Basic Algorithms
      • 1. algorithm principle
      • 2 Division implementation process
    • Performance Analysis of Quick Sort Algorithms
      • 1. Worst case
      • 2. Best Cases
      • 3. Average Condition
    • References

1. Basic algorithm 1.1 algorithm principle

Quick sortingIt is a Sort Algorithm for sub-governance. It divides the array into the left and right parts and sorts them separately. The key lies in the division process. It will rearrange the array so that the following conditions are true:

  • For a division element pointer I, a [I] will be at the final position of the sorted Array
  • A [l],..., The elements in a [I-1] are smaller than those in a [I]
  • A [I + 1],..., The elements in a [r] are larger than those in a [I ].

We sort the sub-files by division, and call this method recursively to process the sub-files. Each division will put at least one element in its final position.

void quick_sort(Item a[], int l, int r){    if(r <= l)        return;    Item v = a[r];    int i = partation(a, l, r);    quick_sort(a, l, i-1);    quick_sort(a, i+1, r);}
1.2 division implementation process
  • 1. Select a [r] as the Division element. This element is located at its final position after Division v = a [r];
  • 2. Scanning starts from the left side of the array until an element greater than the Division element is found. Scanning starts from the right side of the array until an element smaller than the Division element is found.
  • 3. in step 2, the two elements that stop scanning are reversed in the final division array, so the two elements are exchanged.
  • 4. Continue to steps 2 and 3 to ensure that the elements on the left side of the partition element pointer are smaller than those on the v side and those on the right side are larger than those on the v side.
  • 5. When the scan pointer encounters, the Division element is switched to its final position. This division is complete.
    [Note] the above equal elements will also be exchanged, followed by improved algorithms to process repeated Elements
Int partation (Item a [], int l, int r) // division function {int I = L-1, j = r; Item v = a [r]; for (;) {while (less (a [++ I], v); while (less (v, a [-- j]) if (j = l) // v is the minimum break in the array; if (j <= I) // I, j meets break; exch (& a [I], & a [j]);} exch (& a [I], & a [r]); // exchange the Division element to its final position return I ;}

Division process isUnstableBecause each element may be moved to the end of a large number of elements that are equal to it (undetected. So far, no quick sorting based on arrays is stable.

2. Performance Analysis of the Quick Sort Algorithm 2. 1. Worst case

Time Complexity O (N22) Time Comparison

If you call an ordered file of N size, all the division will degrade, the program will call itself N times, each call reduces one element (the left N-1, 1 on the right ......);
N + (N-1) + (N-2) + ...... + 2 + 1 = (N + 1) N2

2.2 Best Cases

Time Complexity O (NlgN) Time Comparison
If each division can split the file into two equal parts, that is, the Division rule is met.
The depth of the recursive tree is LgN , Comparison of each layer O (N)
T (N) = 2 T (N/2) + N
The last F (N) = N Is the price (number of times) used to resolve the problem)
From the case 2 of the Main Theorem, F (N) = N = Nlog22log0N
So T (N) = Nlog22 logs log1N = NlgN

2.3 average

Time Complexity O (NlgN) Time Comparison
Intuitively, it is better to see the best situation and the worst case happen at different layers.
After dividing a difference, a good division is followed. The following figure shows P99, Introduction to algorithms.

References

1. algorithm: C language implementation P192
2. Introduction to algorithms P98
3. Main Theorem: https://www.zybuluo.com/quinn/note/77106

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.