Various sorting algorithm collation

Source: Internet
Author: User

1. Insert Sort

void Insertsort (int *a, int n) {for (i = 1; i < n; i++) {k = a[i];for (j = i; k < a[j-1] && j > 0; j--) a[j] = A[J-1];A[J] = k;}}

2. Bubble sort

void Bubblesort (int *a, int n) {for (i = 0; i < n; i++) {for (j = 0; J < N-i; J + +) {if (A[j] > A[j+1]) swap (a[j], a[j+ 1]);}}}

3. Select sort

void Selectsort (int *a, int n) {for (i = 0; i < n-1; i++) {k = i; for (j = i+1; J < N; j + +) {if (A[j] < a[k]) k = j;} Swap (A[i], a[k]);}}

4. Hill sort

void Shellsort (int *a, int n) {int inc = N;while (inc > 1) {inc = INC/3 + 1;//Insert sort row group element for (int start = 0; start < Inc; start++) {for (i = start + inc; I < n; i + = Inc) {k = A[i];for (j=i; k<a[j-inc]&&j>0; j-=inc) a[j] = a[j-in C];A[J] = k;}}}

5. Base Sorting

 #include <queue> void radixsort (int *a, int n, int d)  {  int digit = 1; queue<int> digitQueue[10];for  ( i = 0;  i < d; ++i)  { for  (j = 0; j < n; j++)  digitqueue[(a[j] / digit)  % 10].push (A[j]); j = 0; for  (int  digitval=0; digitval<10; ++digitval)  while  (!digitqueue[digitval].empty ())  {  a[j] = digitqueue[digitval].front ();  digitqueue[digitval].pop ();  j++; }cout  <<  "number "  << i <<  ":"  << endl;  for  (&NBSP;J&NBSP;=&NBSP;0;&NBSP;J&NBSP;<&NBSP;N;&NBSP;++J)  cout << a[j]  <<  " ";  cout << endl; digit *= 10;//start from single digit until the D-digit  }  } 

6. Heap sort (priority queue)

#define  maxn 100class array {    private:    int  elem[maxn];    public:    int &operator[] (int i)  { return elem[i]; }};class heap {    private:     int n;    array h;    public:     void clear ()  { n = 0; }    int top ()  {  Return h[1]; }    int size ()  { return n; }     void push (int);     void pop ();}; Void heap::p ush (int x)  {//siftup    n = n + 1;     h[n] = x;    int tmp = n;     while (tmp / 2 != 0)   &nbsP; {        if (H[tmp/2] > h[tmp])              swap (H[tmp/2], h[tmp]);         else break;        tmp = tmp/2;     } }void heap::p op ()  {//siftdown    swap (H[1],h[n]);     n = n - 1;    i = 1;     while (2 * i <= n)     {         int parent = i, child = 2 * i;         int small = parent;         if (H[small] > h[child])  small = child;         if (child +  1 <= n && h[child+1] < h[small])  small = child  + 1;        if (small == parent)  break;         else         {             swap (H[parent],h[small]);             i = small;         }    }}

7. Merge sort

Void mergesort (int *a, int x, int y)  // initial x = 0,  y = n{if (y - x <= 1)  return;    //Divide     int m =  (y - x)  / 2 + x;     //conquer    mergesort (a, x, m);     mergesort (a,  M, y);     //combine    int p = x, q =  m, k = 0;    int t[y - x + 1];     while ( p < m | |  q < y)     {        if (q  >= y | |   (P < m && a[p] <= a[q])              t[k++] = a[p++];        else         {             t[k++] = a[q++];             //Reverse Order number               //inverse_number = inverse_number + m - p;         }    }    for (p =  x, k = 0; p < y; p++)  a[p] = t[k++]; }

linked list implementation

struct linkedlist{    int data;    linkedlist *next;};/ /sort the list by mergesort and return the head of itvoid  mergesort (Linkedlist *&head, int len) {    if (len <=  1)  return;    //Divide    int m = len /  2;    linkedlist *mid = head; linkedlist *rear = head,  *new_head = null;    for (int i = 0; i < m  ; i++)     {        mid = mid- >next;        if (i == m - 2)           rear = mid;    }     rear->next = null;    //conquer    mergesort (head, m);//sort the  left side    mergesort (mid, len - m);//sort the right  side    //combine two ordered list    linkedlist  *tmp1 = head, *tmp2 = mid;linkedlist *new_rear = NULL;     while (tmp1 != null | |  tmp2 != null)     {        if (TMP2  == null | | (Tmp1 != null && tmp1->data <= tmp2->data))         {             if (new_head == null)                  new_head = tmp1;            else                  new_rear->next = tmp1;             new_rear = tmp1;             tmp1 = tmp1->next;            }        else          {            if (New_ Head == null)                  new_head = tmp2;             Else                 new_ Rear->next = tmp2;new_rear = tmp2;             tmp2 = tmp2->next;        }    }     head = new_head;}

8. Quick sort

Int partition ( int *a, int low, int high ) {int left, right; int pivot_item = a[low];left = low;right = high;while  ( left  < right )  {/* move left while item <= pivot */ while ( a[left] <= pivot_item && left < high)  left++;/*  move right while item > pivot */while ( a[right] >=  Pivot_item && low < right)  right--;if  ( left <  right )  swap (A[left], a[right]);} /* right is final position for the pivot */swap (A[low], a[right]) ; return right;}                               &nbsP;   void quicksort (Int *a, int low, int high) {int pivot_ Position;if (Low < high) {pivot_position = partition (A, low, high); QuickSort (a, low, pivot_position - 1); QuickSort (A, pivot_position + 1, high);}}

quick-line thought to find the K decimal number

Int first[1000001], second[1000001];int select (int a[], int n, int k)  {    int pivot = a[0];    int st =  0, nd = 0;    for (int i = 1; i < n;  i++)     {        if (a[i] <=  Pivot)             first[st++] = a[i];         else              second[nd++] = a[i];    }    if (St  == k)  return pivot;    else if (st > k)  return  select (first, st, k);     else return select (Second, nd,  k - st -  1);} 

Comparison of sorting algorithms (attention to stability)

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/99/wKiom1asw9eii3gZAACKSdflTP8728.png "title=" Sorting algorithms compare. png "alt=" Wkiom1asw9eii3gzaacksdfltp8728.png "/>



Various sorting algorithm collation

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.