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