插入排序:
/*Author: Eric *Time: 2011.01.07 * *插入排序 */ #include <stdio.h> void insertion_sort(int a[], int n) { int i = 1; int k = 0; while(i < n) { int temp = a[i]; k = i-1; while(k >= 0 && a[k] > temp) { a[k+1] = a[k]; k--; } a[k+1] = temp; i++; } } int main() { int i = 0; int a[] = {7,6,5,4,3,2,1}; insertion_sort(a, 7); printf("a ="); for(i = 0; i < 7; i++) printf(" %d", a[i]); printf("\n"); return 0; }
时间复杂度为Θ(n2)