首页 > 代码库 > 插入排序

插入排序

 1 #include <stdio.h> 2 #include <stdlib.h> 3  4 void insert_sort(int data[], int); 5  6 int main(int argc, char *argv[]) 7 { 8     int i; 9     int data[6] = {5,3,7,10,9,6};10 11     insert_sort(data, 6);12 13     for ( i = 0; i < 6; ++i)14     {15         printf("%d ", data[i]);16     }17 18     printf("\n");19     return 0;20 }21 22 void insert_sort(int data[], int num)23 {24     int i, j;25     26     for (i = 1; i < num; ++i)27     {28         int r = data[i];29         j = i;30 31         while(j > 0 && data[j - 1] > r)32         {33             data[j] = data[j - 1];34             j--;35         }36 /*37         for (j = i; j >= 0; --j)38         {39             if (data[j - 1] > r)40             {41                 data[j] = data[j - 1];42             }43             else44                 break;45         }46  */47         data[j] = r;48     }49 }

 

插入排序