Sort complexity (2)

Source: Internet
Author: User

4. Bubble Sorting:

It is a simple and stable exchange sorting. The sorting process is: Compare the size of adjacent elements. If the sorting requirements are met, the two numbers are not exchanged.

For example, the sequence {30, 12, 52, 23, 15, 65, 31, 58, 20, 63 }:

The following is the first trip: 30 compared with 12, 30> 12 switching location, 30 <52, do not swap location, and so on, put the maximum number in the sequence to the last position, A total of n-1 comparisons were performed. A total of n-1 bubbles are required.

The best time complexity: O (n); cause: if the file is in the forward order, you only need a bubble, n-1 comparison, the number of moves is 0 to complete the sorting. Therefore, the best time complexity is O (n );

Worst time complexity: O (N ^ 2); cause: if the file is in reverse order, n-1 bubbles is required, and n-I keywords are compared each; the maximum value of the comparison is N * (n-1)/2 = O (N ^ 2), and the maximum number of moves is 3n (n-1)/2 = O (N ^ 2) (Note: the number of switches is N * (n-1)/2, and each switch requires three commands: temp = A; A = B; B = temp, this is multiplied by 3), so the worst time complexity is O (n ^ 2 ).

5. Fast sorting: unstable sorting

Example: {49.38.65.97.76.13.27}

First, the sequence is divided into two parts based on 49. The left part is smaller than 49. the right part is greater than 49. This step is shown in the following figure:

Source: http://www.bsgz.net/Article/Class20/Class88/sjjg/200609/936.html

Worst time complexity: O (N ^ 2); cause: When the benchmark selected for each division is the record with the smallest (or largest) keyword in the unordered area, n-1 division is required. The start time of the I-th Division is the time zone length n-I + 1, and the number of comparisons required is n-I, therefore, the total number of comparisons reaches the maximum N * (n-1)/2 = O (N ^ 2), because the number of moves is not greater than the number of comparisons, so the worst time complexity is O (n ^ 2 ).

The best time complexity: O (nlog2n); cause: When the baseline selected for each division is the value, we can use the recursive tree to explain the relationship between the height D of the tree and the number of nodes N:

D ^ 2-1 = N; therefore, the height of the recursive tree d = log2 (n + 1) = O (log2n), and the time complexity of each layer of recursive subnode is O (n) (Note: The total number of times to be compared is less than N, and the number of moves is less than the number of times). Therefore, the time complexity of each layer of recursive subnode * the height of the recursive tree is used to find the total time complexity: O (nlog2n)

6. Merge Sorting: stable sorting

Combine two or more ordered sub-tables into a new ordered table.

Example: {72.28.51.17.96.62.87.33}

  1. {28.72} {17.51} {62.96} {33.87}
  2. {17.28.51.72} {33.62.87.96}
  3. {17.28.33.51.62.72.87.96}

Worst time complexity: O (nlog2n );

Best time complexity: O (nlog2n); log2n dual merging is required, and the merge Time of each trip is O (n). Therefore, the time complexity is O (nlog2n ).

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.