The usual algorithm for cock silk-----

Source: Internet
Author: User

Since the comparison is late, let's talk about the insertion sort today ...

Sorting has an internal sort and an external sort, the internal sort is the data record is sorted in memory, and the external sort is because the sorted data is large, one cannot hold all the sort records at a time, and the external memory needs to be accessed during the sorting process.

Here we talk about eight sorts of sort are internal sort.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/77/wKioL1cBsTWAB2j-AAEeGkSMR7w198.jpg "title=" 1342514529_5795.jpg "alt=" wkiol1cbstwab2j-aaeegksmr7w198.jpg "style=" padding:0px;margin:0px;vertical-align:top; Border:none; "/>


1. Insert Sort---Direct insert sort

There is an ordered sequence of data that requires inserting a number in the already-sorted data sequence, but the data sequence is still ordered after insertion, and a new sorting method is used-insert sort, the basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, In order to obtain a new, number plus one ordered data, the algorithm is suitable for the ordering of a small amount of data, the time complexity of O (n^2). is a stable sorting method. The insertion algorithm divides the array to be sorted into two parts: the first part contains all the elements of the array, except for the last element (where an array has more space to insert), and the second part contains only that element (that is, the element to be inserted). After the first part is sorted, the last element is inserted into the first part of the sequence.

The basic idea of inserting a sort is that each step inserts a record to be sorted, at the size of its key value, into the appropriate position in the previously sorted file until all is inserted.

650) this.width=650; "src=" Http://images.cnblogs.com/cnblogs_com/kkun/201111/201111231433304812.png "alt=" 201111231433304812.png "/>

  #include  <stdio.h>   #define &NBSP;NUM (a)   (sizeof (a)/sizeof (*a))     int insertsort (int *arr, const int n)  {     // Type parameter condition judgment      if (null == arr | |  0 >= n)      {          return -1;     }           int i = 0;    //used to recycle      int j =  0;    //ditto      int k = -1;     //used to record the data taken out of the comparison subscript      int tmp = -1;    // Used to record comparison data      for (i = 1; i < n; i++)       {         k = i;            //record comparison of data subscript           tmp = arr[k];    //record comparison of data           //from small to large sort          for (j  = i - 1;  (0 <= j) && (Tmp < arr[j]);  j--)           {              //data              arr when conditions are met [k] = arr[j];             k =  j;             }          arr[k] = tmp;     }      return&nbsP;0; }  int print_array (const int *arr, const int n)  {      //type parameter condition judgment      if (null == arr | |  0 >= n)      {          return -1;     }           //Traversal array      int i = 0;     for (i =  0; i < n; i++)      {          printf ("%d ",  * (arr+i));     }      printf ("\ n");      return 0; }  int main (void)  {      int arr[] = { 12, 51, 15, 16, 33,  11, 99, 52, 16, 5, 33, 18};     printf ("Before sorting:");      print_array (arr,  num (arr));      insertsort (Arr, num (arr));      printf ("After sorting:");      print_array (Arr, num (arr));           return 0; }   /*  Execution Results:       Before sorting:12 51 15 16 33 11 99 52 16 5 33 18       after sorting: 5 11 12 15 16 16 18 33 33 51 52  99 */



2. Insert Sort Hill Sort

The hill sort (Shell sort) is inserted Sortone of them. is for direct insert sortalgorithmthe improvement. This method is also known as shrinkingIncrementalsort, due to DL. Shell in 1959proposedand the name.

650) this.width=650; "src=" Http://i5.qhimg.com/t01b4af3cd6752197ab.png "alt=" T01b4af3cd6752197ab.png "/>


  #include  <stdio.h>   #define &NBSP;NUM (a)   (sizeof (a)/sizeof (*a))     int insertsort (int *arr, const int n)  {     // Type parameter condition judgment      if (null == arr | |  0 >= n)      {          return -1;     }           int i = 0;    //used to recycle      int j =  0;    //ditto      int k = -1;     //used to record the data taken out of the comparison subscript      int tmp = -1;    // Used to record comparison data      int gap = n;    //increment size       do     {         gap = gap / 3 + 1;          for (I&NBSP;=&NBSP;GAP;&NBSP;I&NBSP;&LT;&NBSP;N;&NBSP;I+=GAP)           {             k  = i;            //record comparison of data subscript               tmp = arr[k];     //Records compare data              //from small to large              for (j = i -  gap;  (0 <= j) && (Tmp < arr[j])  j-=gap)               {                  //data transfer               when conditions are met     arr[k] = arr[j];                  k = j;                  }              arr[k] = tmp;         }      }while (1&NBSP;&LT;&NBSP;GAP);     return 0; }   int print_array (const int *arr, const int n)  {      //type parameter condition judgment      if (null == arr | |  0 >= n)      {          return -1;     }          //traversing an array      int i =  0;     for (i = 0; i < n; i++)       {         printf ("%d ",  * (Arr+i));      }     printf ("\ n");      return  0; }  int main (void)  {     int arr[] =  { 12, 51, 15, 16, 33, 4, 8, 19, 31, 11, 99, 52,  16, 5, 33, 18};     printf ("Before sorting:");      print_array (Arr, num (arr));      insertsort (Arr, num (arr));      printf ("After sorting:");      print_array (Arr, num (arr));           return 0; }   /*      Operation Results:       before sorting:12 51 15 16 33 4 8 19 31 11 99  52 16 5 33 18      sorted After: 4 5 8 11 12 15  16 16 18 19 31 33 33 51 52 99 */


This article is from the "Sea" blog, be sure to keep this source http://lisea.blog.51cto.com/5491873/1761125

The usual algorithm for cock silk-----

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.