![01](https://i-blog.csdnimg.cn/blog_migrate/3f29b24e2b11ace09fb94dc10a3f005b.png)
![02](https://i-blog.csdnimg.cn/blog_migrate/d173736d9132379282bf7402d58911ea.png)
![03](https://i-blog.csdnimg.cn/blog_migrate/910c4fb48ff092aea383a880d884ea54.png)
先写一种最简单直接插入排序:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 void print_array(int *a,int len)/*打印一个数组*/
6 {
7 int i;
8
9 for(i = 0;i < len;i++)
10 {
11 printf("%5d",a[i]);
12 }
13 printf("\n");
14
15 }
16
17 void insert_sort(int *a,int len)/*直接插入排序*/
18 {
19 int i;
20 int j;
21 int current_value;
22 int pos;
23
24 for(i = 1;i < len;i++)/*找到位置*/
25 {
26 current_value = a[i];
27
28 for(j = 0;j < i;j++)/*只要当前值小于前面排好序的。就跳出执行插入*/
29 {
30 if(current_value < a[j])
31 {
32 break;
33 }
34
35 }
36
37
38 if(j < i)/*如果在前面某个位置,则插入,否则即为最大值,不用插入*/
39 {
40 pos = j;
41
42 for(j = i;j > pos;j--)
43 {
44 a[j] = a[j - 1];
45 }
46
47 a[pos] = current_value;
48
49 }
50
51 print_array(a, len);
52
53 }
54
55 }
56
57 int main()
58 {
59 int a[] = {5,4,8,9,6,7,3,-4,50,1};
60 int len = sizeof(a) / sizeof(a[0]);
61 print_array(a,len);
62 printf("\n");
63 insert_sort(a,len);
64 print_array(a,len);
65
66 return 0;
67
68 }
输出结果:
改进:
17 void insert_sort(int *a,int len)/*直接插入排序*/
18 {
19 {
20 int i;
21 int j = 0;
22 int current_value;
23
24 for(i = 1; i < len; i++)
25 {
26 if(a[i] < a[i - 1])
27 {
28 current_value = a[i];
29
30 for(j = i - 1; (j >= 0) && (a[j] > current_value); j--)/*判断条件有两个*/
31 {
32 a[j + 1] = a[j] ;
33 }
34
35 a[j + 1] = current_value;
36 }
37 print_array(a, len);
38 }
39
40 }
41 }
输出结果相同
![05](https://i-blog.csdnimg.cn/blog_migrate/9ee5bb9abc20c7b04a543ad29628ff02.png)
![06](https://i-blog.csdnimg.cn/blog_migrate/a8d729ca6e8037fd5f36f548c8b78689.png)
/*折半插入排序*/
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 void print_array(int *a,int len)/*打印一个数组*/
6 {
7 int i;
8
9 for(i = 0;i < len;i++)
10 {
11 printf("%5d",a[i]);
12 }
13 printf("\n");
14
15 }
16
17 void bin_insert_sort(int *a,int len)/*折半插入排序*/
18 {
19 int i;
20 int j;
21 int current_value;
22 int low;
23 int high;
24 int mid;
25
26 for(i = 1;i < len;i++)
27 {
28 low = 0;
29 high = i - 1;
30 current_value = a[i];
31
32 while(low <= high)/*找到位置*/
33 {
34 mid = (low + high) / 2;
35
36 if(current_value < a[mid])
37 {
38 high = mid - 1;
39 }else
40 {
41 low = mid + 1;
42 }
43 }
44
45 if(current_value > a[mid])
46 {
47 mid = mid + 1;
48 }
49
50 for(j = i;j > mid;j--)/*移动数据*/
51 {
52 a[j] = a[j - 1];
53 }
54
55 a[mid] = current_value;/*插入数据*/
56 print_array(a, len);
57 }
58 }
59 int main()
60 {
61 int a[] = {5,4,8,9,6,7,3,-4,50,1};
62 int len = sizeof(a) / sizeof(a[0]);
63 print_array(a,len);
64 printf("\n");
65 bin_insert_sort(a,len);
66
67 return 0;
68
69 }
![07](https://i-blog.csdnimg.cn/blog_migrate/55ca5d7ea3c3c7a885f261ac79a0edbc.png)
![08](https://i-blog.csdnimg.cn/blog_migrate/d839a710c9a43f0801511a4943945aff.png)
/*简单希尔排序*/
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 void swap(int *a, int *b) /*交换*/
6 {
7 int tmp;
8 tmp = *a;
9 *a = *b;
10 *b = tmp;
11 }
12
13 void print_array(int *a,int len)/*打印一个数组*/
14 {
15 int i;
16
17 for(i = 0;i < len;i++)
18 {
19 printf("%5d",a[i]);
20 }
21 printf("\n");
22
23 }
24
25 void shell_insert_sort(int *v, int len)/*最简单的希尔排序方法*/
26 {
27 int gap;
28 int i;
29 int j;
30
31 for (gap = len / 2; gap > 0; gap /= 2)
32 {
33 for (i = gap; i < len; i++)
34 {
35 for (j = i - gap; j >= 0; j -= gap)
36 {
37 if (v[j] < v[j + gap])
38 {
39 break;
40 }
41
42 swap(v + j, v + j + gap);
43 }
44
45 }
46 print_array(v, len);
47 }
48 }
49
50 int main()
51 {
52 int a[] = {5,4,8,9,6,7,3,-4,50,1};
53 int len = sizeof(a) / sizeof(a[0]);
54 print_array(a,len);
55 printf("\n");
56 shell_insert_sort(a,len);
57
58 return 0;
59
60 }
输出结果:
一种常见的希尔排序:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 void swap(int *a, int *b) /*交换*/
6 {
7 int tmp;
8 tmp = *a;
9 *a = *b;
10 *b = tmp;
11 }
12
13 void print_array(int *a,int len)/*打印一个数组*/
14 {
15 int i;
16
17 for(i = 0;i < len;i++)
18 {
19 printf("%5d",a[i]);
20 }
21 printf("\n");
22
23 }
24
25 void shell_sort(int *v, int len)/*常见的希尔排序*/
26 {
27 int j = 0 ;
28 int i;
29 int current_value;
30 int gap = len;
31
32 do
33 {
34 gap = gap / 3 + 1;
35
36 for(i = gap; i < len; i++)
37 {
38 if(v[i] < v[i - gap])
39 {
40 current_value = v[i];
41
42 for(j = i - gap; (j >= 0) && (v[j] > current_value); j -= gap)
43 {
44 v[j + gap] = v[j];
45 }
46
47 v[j + gap] = current_value;
48 }
49 }
50 print_array(v,len);
51
52 }while(gap > 1);
53 }
54
55 int main()
56 {
57 int a[] = {5,4,8,9,6,7,3,-4,50,1};
58 int len = sizeof(a) / sizeof(a[0]);
59 print_array(a,len);
60 printf("\n");
61 shell_sort(a,len);
62
63 return 0;
64
打印结果:
![](https://i-blog.csdnimg.cn/blog_migrate/92e14326e9a3112c877e7686015d9acb.png)
![13](https://i-blog.csdnimg.cn/blog_migrate/b6eff59de6891f5280d6c41dbe171745.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6ce4b88c10c3f9d938db27ee0f85a843.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2479687699f00129d64823fc5b849712.png)