搜索
逐步观看线性搜索和二分搜索如何寻找目标值:比较、折半,以及命中或未命中。
Linear SearchScan the array one element at a time until the target appears.Binary SearchHalve the search window each step until the target is found.
搜索对比
| 算法 | 最好 | 平均 | 最坏 | 空间 | 需要有序数据? |
|---|---|---|---|---|---|
| Linear Search | O(1) | O(n) | O(n) | O(1) | No |
| Binary Search | O(1) | O(log n) | O(log n) | O(1) | Yes |