4、界下標(biāo)為mid-1的區(qū)間繼續(xù)查找;若x>a[mid],則隨后在下標(biāo)為mid+1,上界下標(biāo)為high的區(qū)間繼續(xù)查找。當(dāng)查找區(qū)間小于等于0時(shí),查找過程結(jié)束。模塊劃分:1.頭文件time.h中包含time()和difftime(end,start)函數(shù),分別實(shí)現(xiàn)取系統(tǒng)當(dāng)前時(shí)間和end減去start的時(shí)間差;2.頭文件stdlib.h中包含rand()函數(shù),實(shí)現(xiàn)隨機(jī)數(shù)的生成;3.voidlist(inta[])實(shí)現(xiàn)對(duì)隨機(jī)數(shù)從小到大的排序;4.intSearch(inta[],intlow,inthigh,
5、intx)用遞歸算法實(shí)現(xiàn)折半查找數(shù)據(jù)元素的函數(shù);5.intBSearch(inta[],intlow,inthigh,intx)用循環(huán)結(jié)構(gòu)實(shí)現(xiàn)折半查找數(shù)據(jù)元素的函數(shù);6.voidmain()主函數(shù),測(cè)試用遞歸算法和循環(huán)結(jié)構(gòu)實(shí)現(xiàn)折半查找數(shù)據(jù)元素的函數(shù)。數(shù)據(jù)結(jié)構(gòu):源程序:#include#includeintBsearch(inta[],intx,intlow,inthigh){intmid;if(low>high)return-1;mid=(low+high)/2;
6、if(x==a[mid])returnmid;elseif(xtest[i])low=i+1;elsehigh=i-1;}if(i>=high)return
7、-1;}voidmain(){time_tstart,end;doubledif;intBsearch(inta[],intx,intlow,inthigh);intCsearch(inttest[],intx,intlow,inthigh);inta[10000],x;intlow=0,high=10000,mid=0;printf("請(qǐng)輸入要查找的元素:");scanf("%ld",&x);printf("x=%ld",x);for(inti=0;i8、;intbn;time(&start);bn=Bsearch(a,x,0,10000);if(bn==-1)printf("x不在數(shù)組a中!");elseprintf("x在數(shù)組a中,下標(biāo)為%d",bn);time(&end);dif=difftime(end,start);printf("遞歸折半方法耗時(shí)為:%f秒",dif);intflag;time(&start);flag=Csearch(a,x,0,10000);if(flag==-1)printf("x不在數(shù)