/*
* Copyright (c).2014, 烟台大学计算机学院
* All rights reserved.
*文件名称:cpp1.cpp
*作 者:董子宾
*完成日期:2015年 11月 30日
*版 本 号:v1.0
*
*问题描述:线性表的折半查找
*输入描述:无
*程序输出:测试结果
*/
问题及代码:
用有序表{12,18,24,35,47,50,62,83,90,115,134}作为测试序列,分别对查找90、47、100进行测试。
<code class="hljs cpp has-numbering"><span class="hljs-preprocessor">#include <stdio.h></span> <span class="hljs-preprocessor">#define MAXL 100</span> <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">int</span> KeyType; <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">char</span> InfoType[<span class="hljs-number">10</span>]; <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> { KeyType key; <span class="hljs-comment">//KeyType为关键字的数据类型</span> InfoType data; <span class="hljs-comment">//其他数据</span> } NodeType; <span class="hljs-keyword">typedef</span> NodeType SeqList[MAXL]; <span class="hljs-comment">//顺序表类型</span> <span class="hljs-keyword">int</span> BinSearch(SeqList R,<span class="hljs-keyword">int</span> n,KeyType k) { <span class="hljs-keyword">int</span> low=<span class="hljs-number">0</span>,high=n-<span class="hljs-number">1</span>,mid; <span class="hljs-keyword">while</span> (low<=high) { mid=(low+high)/<span class="hljs-number">2</span>; <span class="hljs-keyword">if</span> (R[mid].key==k) <span class="hljs-comment">//查找成功返回</span> <span class="hljs-keyword">return</span> mid+<span class="hljs-number">1</span>; <span class="hljs-keyword">if</span> (R[mid].key>k) <span class="hljs-comment">//继续在R[low..mid-1]中查找</span> high=mid-<span class="hljs-number">1</span>; <span class="hljs-keyword">else</span> low=mid+<span class="hljs-number">1</span>; <span class="hljs-comment">//继续在R[mid+1..high]中查找</span> } <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; } <span class="hljs-keyword">int</span> main() { <span class="hljs-keyword">int</span> i,n=<span class="hljs-number">10</span>; <span class="hljs-keyword">int</span> result; SeqList R; KeyType a[]= {<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">9</span>,<span class="hljs-number">12</span>,<span class="hljs-number">32</span>,<span class="hljs-number">41</span>,<span class="hljs-number">45</span>,<span class="hljs-number">62</span>,<span class="hljs-number">75</span>,<span class="hljs-number">77</span>},x=<span class="hljs-number">75</span>; <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i<n; i++) R[i].key=a[i]; result = BinSearch(R,n,x); <span class="hljs-keyword">if</span>(result><span class="hljs-number">0</span>) <span class="hljs-built_in">printf</span>(<span class="hljs-string">"序列中第 %d 个是 %d\n"</span>,result, x); <span class="hljs-keyword">else</span> <span class="hljs-built_in">printf</span>(<span class="hljs-string">"木有找到!\n"</span>); <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }</code><ul style="FILTER: ; ZOOM: 1" class="pre-numbering"><li></li></ul>运行结果: