根据规律插入排序

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a[11] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
 5     int temp, c, b,temp2;
 6     for (c = 0; c < 10; c++)
 7     {
 8         printf("%d ", a[c]);
 9     }
10     scanf("%d", &temp);
11     if (temp>a[9])            //如果比数组最后一位大就插入最后
12     {
13         a[10] = temp;
14     }
15     else
16     {
17         for (b = 0,temp2=0; b < 10; b++)        //如果比最后一位小就遍历前面几位找出比他大的插入
18         {
19             if (temp<a[b])
20             {
21                 temp2 = a[b];
22                 a[b] = temp;
23                 for (c = b+1; c < 11;c++)        //把插入前的那个数取出来然后循环往后放
24                 {
25                     temp = a[c];
26                     a[c] = temp2;
27                     temp2 = temp;
28 
29                 }
30                 break;                //前面修改完后就跳出
31             }
32         }
33 
34     }
35     for (b = 0; b < 11;b++)
36     {
37         printf("%d ", a[b]);
38     }
39     return 0;
40 }

 

posted @ 2020-03-16 15:36  新生代农民工  阅读(125)  评论(0)    收藏  举报