#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAXLENGTH 21 typedef struct { int key; }Record; typedef struct { Record r[MAXLENGTH]; int length; }DataList; void sort(DataList *list) { printf("list length = %d\n",list->length); int i = 1; for(i=1;i<list->length;i++) { printf("list r[%d].key=%d \t",i,list->r[i].key); } printf("\n***********************************************\n"); int j=0; for(i=1;i<list->length;i++) //r[0]为监视哨所以从1开始 { list->r[0].key = list->r[i].key; j=i-1; while(list->r[0].key <= list->r[j].key) { list->r[j+1] = list->r[j]; j--; } list->r[j+1] = list->r[0]; } i = 1; for(i=1;i<list->length;i++) { printf("list r[%d].key=%d \t",i,list->r[i].key); } printf("\n"); } int main() { DataList *list = (DataList *)malloc(1*sizeof(DataList)); memset(list,0,sizeof(DataList)); srand(time(0)); int m = 0; int index = 1; for(m=0;m<20;m++) { int i = 0; i = rand() % 100; list->r[index].key = i; list->length++; index++; } sort(list); getchar(); }