1/*数组的插入排序*/ 2 /*insertion_sort.c*/ 3 4 #include "insertion.h" 5 6 int main (void) 7 { 8 insertion_sort(); 9 10 11 return 0; 12 }
/*insertion.c*/ 1 #include "insertion.h" 2 void insertion_sort(void) 3 { 4 int a[5] = {10,13,55,44,1}; 5 6 int i, j, key; 7 8 9 for (j=1; j<5; j++) 10 { 11 12 printf("%d,%d,%d,%d,%d\n",a[0],a[1],a[2],a[3],a[4]); 13 14 key = a[j]; 15 i=j-1; 16 while (i>=0&&a[i]>key) 17 { 18 a[i+1]=a[i];/*将大的向后移一位*/ 19 i--; 20 } 21 a[i+1] = key;/*将原来小的放前面合适位置*/ 22 } 23 printf("%d,%d,%d,%d,%d\n",a[0],a[1],a[2],a[3],a[4]); 24 25 }/*insertion.h*/ 1#include <stdio.h> 2 3 void insertion_sort (void);
排序之插入排序
最新推荐文章于 2023-10-02 10:50:58 发布