Common sorting methods

Source: Internet
Author: User

Common classic sorting Algorithms 1. Hill sort 2. Two Insert Method 3. Direct insertion Method 4. Direct sequencing with Sentinel 5. Bubble sort 6. Sorting 7. Quick Sort 8. Heap sort one. The Hill (Shell) Sort method (also known as the small incremental sort, which was presented by D.l.shell in 1959)/* Shell Sort Method */#include <stdio.h>void sort (int v[],int n) {     int gap,i,j,temp;     for (Gap=n/2;gap>0;gap/= 2)/* Set the step size of the order, step gap halved each time, until reduced to 1 */     {          for (i=gap ; i<n;i++)  /* navigate to each element */          {              &NBS P;for (J=i-gap; (j >= 0) && (v[j] > V[j+gap])//J-= gap)/* Compare the size of two elements far apart from the gap and decide how to swap */    &NBS According to the sort direction P          {                temp=v[j];                v[j]=v[j+gap];                V[J+GAP]=TEMP;&NB Sp              }         }     }}  two. Two-point insertion method/* Two-part insertion method */void Halfinsertsort (iNT a[], int len) {     int I, j,temp;     int Low, high, mid;     for (i=1; i& Lt;len; i++)      {          TEMP = a[i];/* Save But former element */          Low = 0;          high = i-1;          When (low <= high)/* in A[low...h IGH] Binary find the location of an orderly insert */          {               mid = (low + High)/2; /* Find intermediate elements */               if (A[mid] > Temp)  /* If the intermediate element is larger than the former element, the current element is inserted into the intermediate element Left */               {                HIG h = mid-1;               }               e LSE   &NBSP;/* If the intermediate element is smaller than the current element, but the former element is inserted to the right of the middle element */               {    & nbsp;           low = mid+1;               }    &NBSP ;          /* Find the position of the current element, between low and high */          for (j=i-1; j>high; j--)/* element move */          {           a[j+1] = a[j];    &NB Sp    }          a[high+1] = temp; /* Insert */     }}  three. Direct Insert method/* Direct Insert Method */void insertionsort (int input[],int len)  {      int i,j,temp;     for (i = 1; i < Len; i++)       {          temp = Input[i];  /* operation of the current element, save it in other variables */          for (j = i-1;j>-1&&input[j] > temp; j--)/ * Find a suitable location starting from the previous element of the current element */          {               input[ J + 1] = Input[j]; /* Move elements while looking for */      &nbsp       &NBSP;INPUT[J] = temp;         }     }}  four. Direct sequencing with Sentinel &nbs p;/**     * with Sentinel's direct insert sort, the first element of the array is not used to store valid data      * will input[0] as Sentinel, you can avoid the decision INPUT[J], the array is out of bounds      * because in the process of j--, when J is reduced to 0 o'clock and becomes input[0] compared to input[0]     * itself, it is clear that the number before position I is more than input[i ] Small      * position I on the number does not need to move, directly into the next round of insert comparison.      *     */void insertionsortwithpiquet (int input[],int len)  {      int i,j;     for (i = 2; i < Len; i++)  /* guarantees that the stored data of the first element of the array input is not valid, compared to the elements preceding it from the second data */    & nbsp {          input[0] = input[i];          for (j = i-1; Input[j] > I NPUT[0]; j--)            {              &NBSP;INPUT[J + 1] = input[ j];              &NBSP;INPUT[J] = input[0]; /* INPUT[J] has beenis the largest of the sorted elements */         }     }}  Five. Bubbling method//Bubble Sort method */void bublesort (int a[],int N) {     int i,j,k;     for (j=0;j<n;j++)  /* Bubble method to sort n times */     {          for (i=0;i<n-j;i++)  /* the value of a larger element sinks, only the maximum of the remaining elements can be sunk again */      &NBSP ;   {               if (a[i]>a[i+1])  /* the value of a larger element to the end */    &NB Sp          {                    k=a[i];  &NB Sp                 a[i]=a[i+1];              &NBSP ;     a[i+1]=k;               }         }     }}  Six. Selection sorting method  /* algorithm principle: first with an element as the benchmark, starting from One Direction scanning,  * such as from left to right scanning, to a[0] as the benchmark. Next from a[0] ... Find the smallest element in the a[9] * and swap it with a[0]. The base bit is thenPut the right  * one bit, repeat the above action, for example, to a[1] as the benchmark, find  * a[1]~a[9] the smallest, and a[1] Exchange. The sort ends when the datum bit  * is moved to the last element of the array (at which point all elements on the left of the Datum  * are ordered incrementally, and the datum is the last element, so the sorting is done).  */void selectsort (int a[],int n)  {     int i,j,min,temp;      for (i=0;i <n;i++)       {          min=i;           for (j=i+1;j<=n;j++)  /* the data from J forward is all lined up, so start with J to find the smallest of the remaining elements */          {      & nbsp        if (A[min]>a[j])  /* put the smallest of the remaining elements in A[i] */            &NBS P  {                temp=a[i];             & nbsp   a[i]=a[j];                 a[j]=temp;        &NBSP ;      }         }   } }  seven. Quick Sort/* Quick sort In thismethod,  * n elements are divided into three segments (groups): Left segment left, * right and middle middle. The middle  * contains only one element. The elements in the left segment are less than equal to  * in the middle element, and the elements in the right segment are greater than equals to the  * segment element. So the meta- * in left and right can be sorted independently and do not have to be merged with the sort results of left and  *.  * Use the Quick Sort method to a[0:n-1] sort  * from a[0:n-1] to select an element as middle, * that element is the fulcrum to divide the remaining elements into two segments left * and right, So that the elements in left are less than  * equal to the fulcrum, and the elements in right are greater than or equal to the fulcrum  * recursively use the quick Sort method to sort left  * recursively use the quick Sort method to sort right  * The resulting result is left+middle+right */void quick_sort (int data[],int low,int high)  { int mid;  if (Low

Common sorting methods

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.