第二章 Searching
我觉得既然是仅仅为自己总结,就只抓里面的概要吧,太多了反而不好。
主要讲了三种查找的方法:
1,list search:
顺序查找(sequence search),二分法查找(binary search)
2,hashed list search:
哈希查找(hashed list search)
简单的SeqSearch:
algorithm SeqSearch(val list <array>,
val last <index>,
val target <keyType>,
ref locn <index>) //这里把所有的pre,post,return都省略了
1 looker=0
2 loop(looker < last AND target not equal list[looker])
1 looker=looker+1
3 end loop
4 locn=looker
5 if(target equal list[looker])
1 found=true
6 else
1 found=false
7 end if
8 return found
end algorithm
用C++按照标准的格式写出来就是:
template <class TYPE>
bool SeqSearch(TYPE list[],TYPE last,TYPE target,int& locn)
{
//local definition
int looker=0;
bool found;
//statements
while(looker<last&&target!=list[looker])
{
looker=looker+1;
} //while
locn=looker;
if(target==list[looker])
{
found=true;
}
else
{
found=false;
} //if
return found;
} //SeqSearch
本文地址:http://com.8s8s.com/it/it37625.htm