reference: https://www.cnblogs.com/chengxiao/p/6104371.html
Insertion sort is stable.
Hill sort, is unstable.
An insertion sort is stable and does not change the relative order of identical elements. Shell sorting requires multiple insertion sorting, elements with the same value may be moved in their respective insertion sorting, and finally its stability will be disrupted, so shell sorting is unstable.
Hill sorting, also known as shrinking incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm: incremental gap, which makes the movement of elements jump, and reduces the number of times that elements are moved.
1 #include <stdio.h> 2 3 void insertSort(int a[], int len); 4 void shellSort(int a[], int len); 5 void display(int a[], int len); 6 7 int main(int argc, char *argv) 8 { 9 int a[] = {2,6,7,2,5,9,0}; 10 int len = sizeof(a)/sizeof(a[0]); 11 12 //insertSort(a, len); // insertion sort 13 shellSort(a, len); // Hill sort 14 15 display(a, len); 16 return 0; 17 } 18 19 // Insertion sort: The current value is compared with its previous value, and the part larger than the current value is moved backward as a whole, 20 // Insert the current value into the correct position 21 void insertSort(int a[], int len) 22 { 23 int i; 24 for(i=1; i<len; i++) 25 { 26 int j = i; 27 int temp = a[i]; 28 while(j-1>=0 && temp<a[j-1]) 29 { 30 a[j] = a[j-1]; 31 j--; 32 } 33 a[j] = temp; 34 } 35 } 36 37 // Hill sorting, shrinking incremental sorting 38 // Group by the set increment gap of the target, use insertSort for each group 39 // The code can be implemented without processing by groups, and can be processed across groups one by one 40 void shellSort(int a[], int len) 41 { 42 int gap; 43 for(gap=len/2; gap>=1; gap=gap/2) 44 { 45 int i; 46 for(i=gap; i<len; i++) 47 { 48 int j = i; 49 int temp = a[i]; 50 while(j-gap>=0 && temp<a[j-gap]) 51 { 52 a[j] = a[j-gap]; 53 j = j-gap; 54 } 55 a[j] = temp; 56 } 57 } 58 } 59 60 void display(int a[], int len) 61 { 62 int i; 63 for(i=0; i<len; i++) 64 printf("%d ", a[i]); 65 printf("\n"); 66 }