编译环境:gcc4.4.3
#include <stdio.h> #define MAX 10 void sort(int *sum) { int i, j, key; for(j=1; j<MAX; j++) { key = sum[j]; i = j - 1; while(i>=0 && sum[i]>key) { sum[i+1] = sum[i]; i--; } sum[i+1] = key; } } void sort2(int *sum) { int i, j, key; for(j=1; j<MAX; j++) { key = sum[j]; i = j - 1; while(i>=0 && sum[i]<key) { sum[i+1] = sum[i]; i--; } sum[i+1] = key; } } void prtarry(int* sum) { int i; printf("array: "); for(i=0; i<MAX; i++) { printf("%2d ", sum[i]); } printf("\n"); } int main() { int sum[MAX] = {5,7,8,2,4,6,9,0,1,3}; prtarry(sum); sort(sum); prtarry(sum); return 0; }
转载于:https://www.cnblogs.com/tonykong/archive/2013/02/05/insertion_sort.html
相关资源:数据结构—成绩单生成器