原文链接:https://note.noxussj.top/?source=helloworld
什么是顺序搜索?
顺序搜索是一种比较低效的搜索算法,但是实现起来相对简单。主要步骤如下:
- 遍历数组
- 找到跟目标值相等的元素,就返回它的下标
- 遍历结束后,如果没有搜索到目标值,则返回 -1
基础案例
- 时间复杂度:O (n)
- 空间复杂度:O (1)
1Array.prototype.sequentialSearch = function (target) { 2 for (let i = 0; i < this.length; i++) { 3 if (this[i] === target) { 4 return i 5 } 6 } 7 8 return -1 9} 10 11const res = [1, 2, 3, 4, 5].sequentialSearch(1) // 0
由于代码中遍历了数组,所以时间复杂度为 O (n)。而空间复杂度为 O (1) 因为没有使用会线性增长的变量。
