This is the simplest search algorithm. It's often used as the "baseline" comparison ("The Bad Example" for large data). Unlike Binary Search, it does not require the array to be sorted. 这是最简单、最基础的搜索算法,也是我们用来做对比的"反面教材"(在数据量大时)。 它不需要数组是排好序的。
It's like looking for a book on a messy shelf. You have to pick up the first book, check it, then the second, and so on. If the book is at the end (or not there), you check every single one. 就像在一个乱序的书架上找书。 你只能从第一本开始,拿起来看看是不是,不是就看下一本,直到找到为止。
| Feature特性 | Linear Search线性搜索 | Binary Search二分搜索 |
|---|---|---|
| Pre-condition 前提条件 | None (Unsorted OK) 无 (乱序也行) | Must be Sorted 必须有序 |
| Speed 速度 | Slow O(n) 慢 O(n) | Fast O(log n) 快 O(log n) |
| Implementation 实现难度 | Simple 极简 | Tricky (Boundaries) 稍难 (边界处理) |
| Scenario 适用场景 | Small / Unsorted Data 数据少、无序数组 | Large / Sorted Data 数据多、已排序数组 |