Using C for Shell sorting

Source: Internet
Author: User

The method of shell sorting, also known as the narrowing increment method, is the improvement of the direct insertion sorting method. As for the sorting method implemented after grouping, this example adopts direct selection sorting and direct insertion sort, in theory, the data is basically ordered by grouping, then the direct insertion sort will be better than the direct selection, because the direct selection sort must compare all the elements in each order.

The specific code is as follows:

/**shell Sort*/#include<stdio.h>#defineN 10voidShellchoice (int*list) {    intd=n/2, I,j,k,index;//D is incremental    inttemp;  while(d)//outermost loop, used to determine the size of the step    {         for(i=0; i<d;i++)//outer loop, used to determine how many direct selection sorts to perform        {             for(j=i;j<n;j=j+d)//Inner Loop, sorting after grouping, using direct selection sort{Index=J;  for(k=j;k<n-d;k=k+d) {if(list[index]>list[k+d]) {index=k+D; }                }                if(j==index)Continue; Else{Temp=List[j]; LIST[J]=List[index]; List[index]=temp; } }} D=d/2; }}voidShellinsert (int*list) {        intD,i,j,index;//D is incremental        inttemp;  for(d=n/2;d >0;d =d/2)//outermost loop, used to determine how much the increment        {                 for(i=0; i<d;i++)//outer loop, used to determine how many direct insert sorts to perform                {                         for(j=i;j<n;j=j+d)//Inner Loop, sorted after grouping, sorted by direct insertion{Temp=List[j]; Index=j-D;  while(Temp<list[index] && index>=0) {List[index+d]=List[index]; Index=index-D; } List[index+d]=temp; }                }        }}voidPrintint*list) {    intI=0;  for(i=0; i<n;i++) printf ("%d", List[i]); printf ("\ n");}intMainvoid){    intI=0; inta[n]={0}; printf ("Please enter a 10 number \ n");  for(i=0; i<n;i++) scanf ("%d",&A[i]); printf ("sort before \ n");    Print (a);    Shellinsert (a); printf ("sort after \ n");    Print (a); return 0;}

Using C for Shell sorting

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.