实验报告(7)
实验名称 同组人姓名 实验日期 教师评价: 实验预习□ 实验操作□ 实验结果□ 实验报告□ 其它□ 查找与排序实验 实验性质 实验成绩 □ □ 基本操作 ●验证性 综合性 □设计性 教师签名: 一、实验目的及要求 学生在实习中体会各种查找和内部排序算法的基本思想、适用场合,理解开发高效算法的可能性和寻找、构造高效算法的方法。 二、实验内容 各种基本的查找和排序算法及其实现分析: 1) 实现二分查找算法,并计算相应的ASL。 2) 实现插入排序或选择排序算法。 三、主要设备及软件 WinTc 四、实验流程、操作步骤或核心代码、算法片段 二分查找算法 #include #define EQ(a,b) ((a)==(b)) #define LT(a,b) ((a)<(b)) #define LQ(a,b) ((a)<=(b)) typedef struct{ int key; }selemtype; typedef struct{ selemtype *elem; int length; }sstable; void instruction(void){ printf(\"1 for create table\\n\" \"2 for search table\\n\" \"3 for ASL\\n\"); } int creat(sstable st){ int i,j; selemtype a; for(i=st.length;i>1;i--) for(j=1;j #define EQ(a,b) ((a)==(b)) #define LT(a,b) ((a)<(b)) #define LQ(a,b) ((a)<=(b)) #define MAXSIZE 20 typedef int KeyType; typedef struct{ KeyType key; }redtype; typedef struct{ redtype r[MAXSIZE+1]; int length; }sqlist; void introduct(void){ printf(\"1 for insertsort\\n\"); } void insertsort(sqlist &l){ int i,j; for(i=2;i<=l.length;++i) { if(LT(l.r[i].key,l.r[i-1].key)) { l.r[0]=l.r[i]; l.r[i]=l.r[i-1]; for(j=i-2;LT(l.r[0].key,l.r[j].key);--j) l.r[j+1]=l.r[j]; l.r[j+1]=l.r[0]; } } } void printflist(sqlist &l){ int i; for(i=1;i<=l.length;i++) printf(\"%2d\\\} int main() { int i,key,choice; sqlist L; introduct(); while(choice!=-1) { printf(\"enter the choice\"); scanf(\"%d\ switch(choice) { case 1: printf(\"enter the length:\"); scanf(\"%d\ printf(\"enter the keys:\"); for(i=1;i<=L.length;i++){ scanf(\"%2d\ } printf(\"the list which insertsort is:\"); insertsort(L); printflist(L); printf(\"\\n\"); break; } } return 0; } 五、实验结果的分析与评价 二分查找 插入排序