Direct Insert Sort, binary insert sort, shell sort, bubble sort, select sort

Source: Internet
Author: User

first, direct insertion sortStability, time complexity: preferably O (n), worst O (n^2), average O (n^2). Space complexity O (1)

void Insertsort (int l[], int n) {int I, j,key;for (i = 1; i<n; i++) if (L[i] < l[i-1])//need to insert l[i] into ordered table L[0...i-1]{key = L [I];for (j = i-1; J >= 0 && Key < l[j]; j--)//post-shift l[j+1] = l[j]; L[J+1] = key;//inserted into the correct position}}

two or two points insert sortFinds the insertion position using a binary search. Stability. Best Case o (n lg N), worst and Average case O (n^2), Spatial complexity O (1).


void Binsertsort (int l[], int n) {int i, J,key, Low, Mid, high;for (i = 1; i < n; i++) {key = L[i];low = 0; high = I-1;WH Ile (Low <= High)//in ordered L[low,..., High] binary find the position of an orderly insert {mid = (Low+high)/2;if (Key < L[mid]) high = Mid-1;elselow = mid + 1;} for (j = i-1; j>=high+1;j--)//back shift//j >= lowl[j+1] = l[j]; L[HIGH+1] = key;//Insert key   //l[low] = key}}

Iii. sort of HillUnstable, the time complexity is in the ideal case O (NLGN), the worst case is O (n^2). Space complexity O (1)

void Shellsort (int l[], int n) {int gap = N,i, J, Tmp;int K1=0, K2;while (Gap > 1)//trip Shell sort {K1++;k2=0;gap = Gap/3+1;fo R (i = Gap; i < n; i++) if (L[i] < L[i-gap]) {k2++;tmp = L[i];for (j = i-gap;j>=0 && tmp < L[J]; J-= Gap) L [J+gap] = l[j]; L[J+GAP] = tmp;//printf ("gap=%d,%d_%d:", Gap,k1, K2); Print (L, n);}}}

Four, bubble sort

Stability, time complexity best O (n), worst and Average condition O (n^2). Space complexity O (1).


void Bubblesort (int l[], int n) {bool Exchange;int I, j;for (i = 0; i < n; i++) {exchange = False;for (j = n-1; J >i; J -) if (L[j] < l[j-1])//Here is a small number that has been exchanged {Std::swap (l[j], l[j-1]); exchange = true;} if (!exchange) break;}}

Bidirectional bubble Sort:

Improved version of bubble sort (bidirectional bubbling) void Bidbubblesort (int a[], int n) {    int left, right, T, L, R, J, I = 0;      left =0;    right = n-1;     //Two-way bubbling algorithm, Greatly reduces the number of cycles to sort     while (left < right)     {         //must be assigned to L and R, otherwise the R in Right=r is not assigned if the array is ordered at the beginning. That is, error         l = left + 1;        r = right-1;          //first loop put the maximum value at the end          for (j = left, J < right; J + +)         {             if (A[j] > a[j + 1])              {                 t = A[J];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;A[J] = a[j + 1];                 a[j + 1] = t;                 r = j;             }        }         right = r;          The second cycle places the smallest value at the beginning         for (j = right; J > left; j--)          {            if (A[j] < a[j-1])             {                 t = a[j];     & nbSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;A[J] = a[j-1];      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;A[J-1] = t;                 l = j;             }        }         left = l;         printf ("%d order results:", i + 1);         i++;        for (j = 0; j < n; J + +) {            printf ("%d\t", A[j]);         }    }     printf ("Finally sort result:");     for (j = 0; J < N; j + +) {        printf ("%d\ t ", a[j]);     }} 

v. Simple selection of sorting

not stable. Time complexity O (n^2).

Space complexity O (1).

void Slectsort (int l[], int n) {int I, J, index;for (i = 0; i < n-1; i++) {index = i;for (j = i+1; J < N; j + +) if (L[j] &l T L[index]) index = J;IF (Index! = i) Std::swap (L[i], L[index]);}}
References: http://blog.csdn.net/han_xiaoyang/article/details/12163251#t128

Three vernacular classic Algorithm series Shell sort implementation

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Direct Insert Sort, binary insert sort, shell sort, bubble sort, select sort

Related Article

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.