📄 page_651.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_651</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_650.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_651</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_652.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 651</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">To see how the While loop and the subsequent assignment statement work, let's look at the two possibilities: either </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">item</font><font face="Times New Roman, Times, Serif" size="3"> is in the list or it is not. If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">item</font><font face="Times New Roman, Times, Serif" size="3"> is in the list, the loop terminates when the expression </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">index < length</font><font face="Times New Roman, Times, Serif" size="3"> is TRUE and the expression </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">item != list[index]</font><font face="Times New Roman, Times, Serif" size="3"> is FALSE. After loop exit, the variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">found</font><font face="Times New Roman, Times, Serif" size="3"> is therefore assigned the value of the expression </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">index < length</font><font face="Times New Roman, Times, Serif" size="3">, which is TRUE. On the other hand, if </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">item</font><font face="Times New Roman, Times, Serif" size="3"> is not in the list, the loop terminates when the expression </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">index < length</font><font face="Times New Roman, Times, Serif" size="3"> is FALSE-That is, when </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">index</font><font face="Times New Roman, Times, Serif" size="3"> becomes equal to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length</font><font face="Times New Roman, Times, Serif" size="3">. Subsequently, the value assigned to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">found</font><font face="Times New Roman, Times, Serif" size="3"> is FALSE (see Figure 12-1).</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">We can use this sequential search function in any program requiring a list search. In the form shown, it searches a list of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ItemType</font><font face="Times New Roman, Times, Serif" size="3"> components, provided that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ItemType</font><font face="Times New Roman, Times, Serif" size="3"> is an integral type. When the function is used with a list of floating point values, it must be modified so that the While statement tests for near equality (for the reasons discussed in Chapter 10). In the following statement, it is assumed that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">EPSILON</font><font face="Times New Roman, Times, Serif" size="3"> is defined as a global constant.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">while聽(index聽<聽length聽&&聽fabs(item聽-聽list[index])聽>=聽EPSILON)<br />聽聽聽聽index++;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">The sequential search algorithm finds the first occurrence of the searched-for item. How would we modify it to find the last occurrence? We would initialize </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">index</font><font face="Times New Roman, Times, Serif" size="3"> to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length-1</font><font face="Times New Roman, Times, Serif" size="3"> and decrement </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">index</font><font face="Times New Roman, Times, Serif" size="3"> each time through the loop, stopping when we found the item we wanted or when </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">index</font><font face="Times New Roman, Times, Serif" size="3"> became-1.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Before we leave this search algorithm, let's introduce a variation that makes the program more efficient, although a little more complex. The</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td align="center"><font face="Times New Roman, Times, Serif" size="3"><img src="e47d67a3a6bb3cf11cd7be32fe3f6753.gif" border="0" alt="0651-01.gif" width="431" height="274" /></font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td align="center"><font face="Times New Roman, Times, Serif" size="2">Figure 12-1<br />Generalized Sequential Search</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_650.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_651</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_652.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -