数据结构实验---排序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include"stdio.h"

#include"string.h"

typedef struct StudentInfo

{

char ID[11];

char * name;

float score;

}StudentInfo;

StudentInfo StuInfo[12]=

{

{"0800301105", "JACK", 95},

{"0800201505", "LUN", 85},

{"0400820115", "MARY", 75.5},

{"0400850122", "KATE", 78.9},

{"0500201011", "LILI", 88},

{"0800401105", "JACK", 96},

{"0600830105", "JAN", 98.4},

{"0952520012", "SAM", 75},

{"9721000045", "OSCAR", 64},

{"0700301105", "JACK", 97},

{"0458003312", "ZOE", 68.9},

{"0400830211", "BOBI", 87.6} };

int gtid(StudentInfo* a,StudentInfo* b) {

return strcmp(a->ID,b->ID)>0;

}

int itid(StudentInfo* a,StudentInfo* b) {

return strcmp(a->ID,b->ID)<0;

}

int gtname(StudentInfo* a,StudentInfo* b) {

return strcmp(a->name,b->name)>0; }

int itname(StudentInfo* a,StudentInfo* b) {

return strcmp(a->name,b->name)<0; }

int gtscore(StudentInfo* a,StudentInfo* b) {

return (a->score)>(b->score);

}

int itscore(StudentInfo* a,StudentInfo* b)

{

return (a->score)<(b->score);

}

void

InsertionSort( StudentInfo A[], int N ,int g(StudentInfo* ,StudentInfo* )) {

int j, P;

StudentInfo Tmp;

for( P = 1; P < N; P++ )

{

Tmp = A[ P ];

for( j = P; j > 0 && g(&A[ j - 1 ],&Tmp); j-- )

A[ j ] = A[ j - 1 ];

A[ j ] = Tmp;

}

}

void

Shellsort( StudentInfo A[ ], int N ,int gt(StudentInfo* ,StudentInfo* )) {

int i, j, Increment;

StudentInfo Tmp;

for( Increment = N / 2; Increment > 0; Increment /= 2 )

for( i = Increment; i < N; i++ )

{

Tmp = A[ i ];

for( j = i; j >= Increment; j -= Increment )

if( gt(&A[ j - Increment ],&Tmp) )

A[ j ] = A[ j - Increment ];

else

break;

A[ j ] = Tmp;

}

}

void initStudentInfo(StudentInfo a[])

{

int i;

for(i=0;i<12;i++)

a[i]=StuInfo[i];

}

void ptinStudentInfo(StudentInfo a[])

{

int i;

for(i=0;i<12;i++)

printf("%s %6s %.2f\n",a[i].ID,a[i].name,a[i].score); }

main()

{

StudentInfo now[12];

printf("原始数据:\n");

initStudentInfo(now);

ptinStudentInfo(now);

printf("插入排序,学号递增:\n");

initStudentInfo(now);

InsertionSort( now, 12 ,gtid);

ptinStudentInfo(now);

printf("插入排序,名字递增:\n");

initStudentInfo(now);

InsertionSort( now, 12 ,gtname);

ptinStudentInfo(now);

printf("快速排序,名字递增:\n");

initStudentInfo(now);

Shellsort( now, 12 ,gtname);

ptinStudentInfo(now);

printf("快速排序,成绩递增:\n");

initStudentInfo(now);

Shellsort( now, 12 ,gtscore);

ptinStudentInfo(now);

}

相关文档
最新文档