算法8 顺序查找
基本思想: 一列数放在数组a(1)---a(n)中,待查找的数放在x 中,把x与a数组中的元素从头到尾一一进行比较查找。用变量p表示a数组元素下标,p初值为1,使x与a(p)比较,如果x不等于a(p),则使p=p+1,不断重复这个过程;一旦x等于a(p)则退出循环;另外,如果p大于数组长度,循环也应该停止。
代码如下(通过定义一个自定义函数Find来实现):
Private Function Find( a( ) As Single,x As Single) As Integer Dim n%,p% n=Ubound( a ) p = 1 Do While x <> a(p) And p < =n p = p + 1 Loop If p>n then p=0 Find=p End Function