1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| void insertSort(int array[], int length) { int i, j, tmp; for(i = 1; i < length; i++) { if(array[i] < array[i-1]) { tmp = array[i]; for(j = i-1; array[j] > tmp; j--) array[j+1] = array[j]; array[j+1] = tmp; } } }
int main(void) { int a[] = {-100, 4, 6, -10, 9, 46, -4, 3, 6, 5, 8, 44, 20}; int n = sizeof a /sizeof a[0]; int i; for (i = 0; i < n; i++) printf("%d%s", a[i], i == n -1 ? "\n" : " "); insertSort(a, n); for (i = 0; i < n; i++) printf("%d%s", a[i], i == n -1 ? "\n" : " ");
return 1; }
|
本文为作者原创文章,未经作者允许不得转载。