这是我看算法导论接触的第一个算法
1 #include2 #include 3 void insert_sort(int a[], int length) 4 { 5 int key; 6 int j; 7 8 for (int i = 1; i < length; i++) 9 {10 key = a[i];11 j = i - 1;12 13 while (a[j] >key && j >= 0)14 {15 a[j + 1] = a[j];16 j--;17 }18 a[j + 1] = key;19 20 }21 for (int i = 0; i < length; i++)22 {23 printf("%d ",a[i]);24 }25 26 }27 int main()28 {29 int matrix[6] = { 31,41,59,26,41,58 };30 insert_sort(matrix, 6);31 return 0;32 33 }