Data structure Learning notes 06 sort (bubbling, inserting, Hill)

Source: Internet
Author: User

Premise
void X_sort (ElementType a[], int N)
In most cases, for the sake of simplicity, discuss sorting from small integers
n is a positive integer
Only discuss comparison-based sorting (> = < defined)
Only internal sorting is discussed
Stability: Any two equal data, the relative position before and after the order does not change

1. Bubble sort

(sort from small to large)

Physical meaning: Big bubbles go down, small bubbles go up

Each time compares adjacent two bubbles, meets the condition, the exchange position, each round compares, the biggest bubble sinks to the bottom.

Best case: Order t = O (N)
Worst case: Reverse t = O (n^2)

Stability

1#include <stdio.h>2 3typedefintElementType;4 5 voidBubblesort (ElementType a[],intN)6 {7      for(intP = n1; P >=0; p--) {8         intFlag =0;9          for(inti =0; i < P; i++) {Ten             if(A[i] > a[i+1] ) { OneElementType temp = a[i];//swap a[i] a[i+1] AA[i] = a[i+1]; -a[i+1] =temp; -Flag =1; the             } -         } -         if(Flag = =0) -              Break; +     } - } +  A intMain () at { -     intA[] = { the,8, -,Wuyi, +, +}; -Bubblesort (A,6); -      for(inti =0; I <6; i++) -printf"%d", A[i]); -      in     return 0; -}
Bubblesort

2. Insert Sort

(sort from small to large)

And playing poker almost, every touch of the card from the final step by step to compare, need to insert a small, forward comparison, find the right place to insert.

Best case: Order t = O (N)
Worst case: Reverse t = O (n^2)

1#include <stdio.h>2 3typedefintElementType;4 5 voidInsertionsort (ElementType a[],intN)6 {     7     inti;8      for(intP =1; P < N; p++ ) {9ElementType temp = a[p];//Take out the first element in an unordered sequenceTen          for(i = P; i >0&& a[i-1] > temp; i-- ) OneA[i] = a[i-1];//Compare and move the elements in the sorted sequence to the right AA[i] =temp; -     } - } the  - intMain () - { -     intA[] = { the,8, -,Wuyi, +, +}; +Insertionsort (A,6); -      for(inti =0; I <6; i++) +printf"%d", A[i]); A     return 0; at}
Insertionsort

3. Hill sort

Insert sort every 5 3 1 intervals.

5 3 10% is an incremental sequence.

Define incremental sequence DM > DM-1 > ... > D1 = 1
The "dk-interval" for each Dk is sorted (k = M, M-1, ...). 1)
"dk-interval" ordered sequence, after performing the "dk-1-interval" sort, is still "dk-interval" ordered

Original Hill sort DM = [N/2] rounding down, Dk = [D (k+1)/2] rounding down

Worst case scenario: T =0 (n^2)

If the increment element is not coprime, then small increments may not work at all.

More incremental sequences
Hibbard Increment sequence
Dk = 2^k–1-Adjacent element coprime
Worst case: T = O (n^ 3/2)
Conjecture: Tavg = O (n^ 5/4)
Sedgewick Increment sequence
{1, 5, 19, 41, 109, ...}
9*4^i–9*2^i + 1 or 4^i–3*2^i + 1
Conjecture: Tavg = O (n^ 7/6), Tworst = O (n^ 4/3)

1#include <stdio.h>2 3typedefintElementType;4 5 /*Original Hill Sort*/ 6 voidShell_sort (ElementType a[],intN)7 {8     inti;9      for(intD = n/2; D >0; D/=2) {Ten          for(intP = D; P < N; p++) { OneElementType temp =A[p]; A              for(i = P; I >= D && a[i-d]>temp; i-=D) -A[i] = a[i-D]; -A[i] =temp; the         } -     } - } -  + /*Hill sort-with Sedgewick increment sequence*/ - voidShellsort (ElementType a[],intN) + {  A      intSi, I; at      /*only a small percentage of increments are listed here*/ -      intSedgewick[] = {929,505,209,109, A, +,5,1,0}; -        -       for(Si =0; Sedgewick[si] >= N; si++ )  -;/*initial increment Sedgewick[si] cannot exceed the length of the sequence to be ordered*/ -   in       for(intD = Sedgewick[si]; D >0; D = sedgewick[++Si]) -           for(intP = D; P < N; p++) {//Insert Sort toElementType temp =A[p]; +               for(i = P; I >= D && a[i-d]>temp; i-=D) -A[i] = a[i-D]; theA[i] =temp; *          } $ }Panax Notoginseng  - intMain () the { +     intA[] = { the,8, -,Wuyi, +, +}; AShell_sort (A,6); theShellsort (A,6); +      for(inti =0; I <6; i++) -printf"%d", A[i]); $     return 0; $}
Shellsort

Data structure Learning notes 06 sort (bubbling, inserting, Hill)

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.