"Data structure Seventh Week" sort (top)

Source: Internet
Author: User

1. Simple sorting

void Bubble_sort (Elementype a[], int N) {for (P = N-1; P >= 0; p--) {flag = 0;for (i = 0; i < P; ++i) {if (A[i] > A[i+1]) {Swap (a[i],a[i+1]); flag = 1;}} if (flag = = 0) {break;}}}

Best case: Order t = O (N)

Worst case: Reverse T = O (n^2)

2. Insert Sort

void Insertion_sort (ElementType a[], int N) {for (P = 1; P < N; ++P) {tmp = a[p];for (i = P; i > 0 && a[i-1] > Tmp;-I.) {A[I] = a[i-1];} A[i] = Tmp;}}

Best case: Order t = O (N)

Worst case: Reverse T = O (n^2)

Theorem: A sequence consisting of any n different elements has an average of n (N-1)/4 reverse pairs.

Theorem: The average time complexity of any algorithm that is ordered only by exchanging adjacent two elements is Ω (n^2).

To improve the efficiency of the algorithm, we must eliminate more than 1 reverse order every time! 2 elements at a distance from each exchange!

  

"Data structure Seventh Week" sort (top)

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.