C语言Ex7_9折半查找算法.pptVIP

  • 4
  • 0
  • 约1.23千字
  • 约 7页
  • 2020-06-26 发布于广东
  • 举报
查找算法;解: 从表列中查一个数最简单的方法是从第1个数开始顺序查找,将要找的数与表列中的数一一比较,直到找到为止。如果表列中无此数,则应找到最后一个数,然后判定“找不到”。 但这种“顺序查找法”效率低,如果表列中有1000个数,且要找的数恰恰是第1000个数,则要进行999次比较才能得到结果。平均比较次数为500次。;折半查找法是效率较高的一种方法。基本思路如下: 假如有已按由小到大排好序的9个数,a[1]~a[9],其值分别为: 1, 3, 5, 7, 9, 11, 13, 15, 17;#includestdio.h #define N 15 void main( ) { int i,number,top,bott,mid,loca,a[N],flag=1,sign; char c; printf(Enter data:\n); scanf(%d,a[0]); i=1; while(iN) { scanf(%d,a[i]); if(a[i]a[i-1]) i++; else printf(Enter the data again:\n); }; printf(\n); for(i=0;iN;i++)printf(%4d,a[i]); printf(\n); while(fla

文档评论(0)

1亿VIP精品文档

相关文档