插入排序之直接插入排序

 1 void insert_sort()
 2 {
 3     int i,j;
 4     unsigned char array[10] = {4,1,3,9,6,2,8,5,0,7};
 5     unsigned char len = sizeof(array);
 6     
 7     /*遍历所有无序序列*/
 8     for(i = 1; i< len; i++)
 9     {
10         unsigned char temp = array[i];
11         /*遍历所以有序序列,排序*/
12         for(j=i-1; j>=0 && temp < array[j]; j--)
13         {
14             array[j+1] = array[j];
15         }
16         array[j+1] = temp;
17     }
18     for(i=0;i<10;i++)
19     {
20         printf("%d\n",array[i]);
21     }
22 }

 

posted @ 2023-08-28 16:49  njit-sam  阅读(6)  评论(0编辑  收藏  举报