Sequential Search
Idea
Sequential Search scans from one end, comparing keys one by one until the target is found or the scan ends. Works for unordered or ordered linear lists; best for small data or low efficiency requirements.
顺序查找法演示
37195
Algorithm
Pseudo-code:
for i = 0 to n-1:
if A[i] == x:
return i // found
default:
return -1 // not found
Complexity
- Best: (first element matches)
- Worst: (last or absent)
- Average:
Exercises
Exercise 1
Given , use sequential search to find .
参考答案
思路:逐个比较直到命中。
步骤:
- 比较 不是
- 比较 不是
- 比较 不是
- 比较 找到
答案:下标 。
Exercise 2
适用结构与平均时间复杂度?
参考答案
思路:场景与复杂度。
步骤:
- 适用于无序/有序线性表
- 平均时间复杂度
答案:线性表,平均 。
Past exam
2021·408
长度为 的顺序表查找 :成功则与后继交换;失败则插入表尾。写算法并分析平均时间复杂度。
参考答案
思路:顺序查找 + 交换/插入;算平均比较次数。
步骤:
- 从表头顺序查找
- 找到后与后继交换
- 未找到则插入表尾
- 平均比较 ,失败需 次
- 平均时间复杂度
答案:如上,平均 。