⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c75_41.htm

📁 经典c语言教程
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>二分查找的算法</title>
</head>

<body bgcolor="#ccefcc">

<blockquote>
  <p>在这个算法中, 我们将在一个包含 n 个元素的数组 M 
  中查找一个元素 x。 算法假设 M 已经按升序排列了。<br>
  <br>
  二分搜索算法 <br>
  第一步: 将 low 置为0, high 置为 n-1; <br>
  第二步: 如果 low&gt;high 那么 x 不在 M 中, 算法结束。<br>
  第三步: 将 mid 设为 (low+high)/2;<br>
  第四步: 如果 M[mid]&lt;x 那么将 low 设为 mid+1 并且转向第二步; <br>
  第五步: 如果 M[mid]&gt;x 那么将 high 设为 mid-1 并且转向第二步; <br>
  第六步: M[mid] 与 x 相等算法结束。<br>
  <br>
  我们可以使用这个算法写一个函数, 在这个字典中查找一个词, 
  如果找到, 返回项目号否则返回 -1。</p>
  <div align="center"><center><table border="6" width="670" cellspacing="0" cellpadding="6"
  height="150" bordercolor="#FF9933">
    <tr>
      <th width="894" bgcolor="#FF9933">程序</th>
    </tr>
    <tr>
      <td ALIGN="center" width="894" bgcolor="#00FFFF"><p align="left">int lookup(struct entry 
      dictionary[],char search[],int num)<br>
      {<br>
      &nbsp;&nbsp;&nbsp; int low=0, high=num-1;<br>
      &nbsp;&nbsp;&nbsp; int mid, result;<br>
      &nbsp;&nbsp;&nbsp; while (low &lt;= high)<br>
      &nbsp;&nbsp;&nbsp; {<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mid = (low + high)/2;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result=strcmp(dictionary[mid].word, search);<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (result == -1) low = mid+1;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (result == 1) high=mid-1;<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else return(mid);/* found it */<br>
      &nbsp;&nbsp;&nbsp; }<br>
      &nbsp;&nbsp;&nbsp; return(-1); /* not found */<br>
      }<br>
      <br>
      main()<br>
      {<br>
      &nbsp;&nbsp;&nbsp; static struct entry dictionary[100] ={{&quot;aardvark&quot;,&quot;a 
      burrowing African mammal&quot;},<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&quot;abyss&quot;, &quot;a bottomless 
      pit&quot; },{&quot;acumen&quot;, &quot;mentally sharp; keen&quot; },<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&quot;addle&quot;,&quot;to become 
      confused&quot; },{&quot;aerie&quot;, &quot;a high nest&quot;}, ...... ...... };<br>
      &nbsp;&nbsp;&nbsp; char word[10];<br>
      &nbsp;&nbsp;&nbsp; int entrys = 10,num;<br>
      &nbsp;&nbsp;&nbsp; printf(&quot;Enter word:\n&quot;);<br>
      &nbsp;&nbsp;&nbsp; scanf(&quot;%s&quot;,word);<br>
      &nbsp;&nbsp;&nbsp; num = lookup(dictionary,word,entrys);<br>
      &nbsp;&nbsp;&nbsp; if (num != -1)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf(&quot;%s\n&quot;,dictionary[num].definition);<br>
      &nbsp;&nbsp;&nbsp; else printf(&quot;Sorry,that word is not in my dictionary.&quot;);<br>
      }</td>
    </tr>
  </table>
  </center></div><p align="center"><a href="javascript:close()">关闭</a></p>
</blockquote>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -