线性搜索算法
线性搜索是一种非常简单的搜索算法。在这种类型的搜索中,逐个对所有项目进行顺序搜索。检查每个项目,如果找到匹配项,则返回该特定项目,否则搜索将继续,直到数据收集结束。
算法
Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit
伪代码
procedure linear_search (list, value) for each item in the list if match item == value return the item's location end if end for end procedure