欢迎来到天天文库
浏览记录
ID:50865524
大小:14.50 KB
页数:2页
时间:2020-03-15
《查找(完整可运行代码).doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、设有序序列的数据元素为:(3,10,13,17,40,43,50,70)1)编写顺序查找函数2)编写二分查找函数3)在主程序中输入关键字(43和5),分别调用两种查找函数,输出结果。#include#defineMAX30typedefintelemtype;typedefintkeytype;intseq_search(elemtypeS[],keytypek,intn){inti;S[n]=k;i=0;while(S[i]!=k)i++;if(i2、;}else{printf("searchingfailed...");return(-1);}}intbin_search(elemtypeS[],keytypek,intn){intlow,high,mid;low=0;high=n-1;while(low<=high){mid=(low+high)/2;if(S[mid]==k){printf("searchingsuccess!");returnmid;}elseif(S[mid]3、.");return(-1);}intmain(){elemtypeS[MAX];keytypekeyval;intnum;printf("inputthelengthofS[](<=%d):",MAX);scanf("%d",&num);printf("inputthevaluesofS[]:");for(inti=0;i4、val!=-1){printf("theresultofseq_search:");seq_search(S,keyval,num);printf("theresultofbin_search:");bin_search(S,keyval,num);printf("inputkey-value:");scanf("%d",&keyval);}return0;}
2、;}else{printf("searchingfailed...");return(-1);}}intbin_search(elemtypeS[],keytypek,intn){intlow,high,mid;low=0;high=n-1;while(low<=high){mid=(low+high)/2;if(S[mid]==k){printf("searchingsuccess!");returnmid;}elseif(S[mid]3、.");return(-1);}intmain(){elemtypeS[MAX];keytypekeyval;intnum;printf("inputthelengthofS[](<=%d):",MAX);scanf("%d",&num);printf("inputthevaluesofS[]:");for(inti=0;i4、val!=-1){printf("theresultofseq_search:");seq_search(S,keyval,num);printf("theresultofbin_search:");bin_search(S,keyval,num);printf("inputkey-value:");scanf("%d",&keyval);}return0;}
3、.");return(-1);}intmain(){elemtypeS[MAX];keytypekeyval;intnum;printf("inputthelengthofS[](<=%d):",MAX);scanf("%d",&num);printf("inputthevaluesofS[]:");for(inti=0;i4、val!=-1){printf("theresultofseq_search:");seq_search(S,keyval,num);printf("theresultofbin_search:");bin_search(S,keyval,num);printf("inputkey-value:");scanf("%d",&keyval);}return0;}
4、val!=-1){printf("theresultofseq_search:");seq_search(S,keyval,num);printf("theresultofbin_search:");bin_search(S,keyval,num);printf("inputkey-value:");scanf("%d",&keyval);}return0;}
此文档下载收益归作者所有