排序之插入排序

View Code
 1 #include <stdio.h>

 2 void InsertSort(int *a,int length);

 3 int main()

 4 {

 5     int a[8] = {5,8,7,10,25,64,3,1};

 6     InsertSort(a,8);

 7     for(int i = 0;i<8;++i)

 8     {

 9         printf("%d ",a[i]);

10     }

11     printf("\n");

12     return 0;

13 }

14 void InsertSort(int *a,int length)

15 {

16     int t = 0;

17     

18     for(int i = 1;i < length ;++i)

19     {

20         t = a[i];

21         a[i] = a[i-1];

22         int j = i;

23         while(j > 0 && (t < a[j -1]))

24         {

25             a[j] = a[j - 1] ; 

26             j--;

27         }

28         a[j] = t;

29     }

30 }

 

你可能感兴趣的:(插入排序)