Improved insert sorting
/* Note: The insert sorting method extracts a value from the front end of the half-end of the unordered sequence and inserts the appropriate position of the first half of the sorted sequence. The concept is simple but the speed is not high. One of the basic principles of sorting acceleration is to make the results of the previous sorting as much as possible to speed up the sorting, shell sorting method is based on this concept to improve the insert sorting method. Solution: slightly */# include <stdio. h> # include <stdlib. h> # include <time. h> # define MAX 10 # define SWAP (x, y) {int t; t = x; x = y; y = t;} void shellsort (int []); int main (void) {int number [MAX] = {0}; int I; srand (time (NULL); printf ("Before sorting: \ n "); for (I = 0; I <MAX; I ++) {number [I] = rand () % 100; printf ("% d", number [I]);} shellsort (number); // system ("pause"); return 0;} void shellsort (int number []) {int I, j, k, gap, t; gap = MAX/2; printf ("\ n after sorting: \ n"); while (gap> 0) {for (k = 0; k <gap; k ++) {for (I = k + gap; I <MAX; I + = gap) {for (j = I-gap; j> = k; j-= gap) {if (number [j]> number [j + gap]) {SWAP (number [j], number [j + gap]) ;}else {break ;}}}} printf ("\ n gap = % d:", gap); for (I = 0; I <MAX; I ++) {printf ("% d ", number [I]);} printf ("\ n"); gap/= 2 ;}}