2023-02-16 03:39:01 +08:00
|
|
|
|
# 线性查找
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
「线性查找 Linear Search」是一种简单的查找方法,其从数据结构的一端开始,逐个访问每个元素,直至另一端为止。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 算法实现
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
例如,若我们想要在数组 `nums` 中查找目标元素 `target` 的对应索引,可以采用线性查找方法。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![在数组中线性查找元素](linear_search.assets/linear_search.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linear_search.java"
|
2023-02-07 19:05:19 +08:00
|
|
|
|
[class]{linear_search}-[func]{linearSearchArray}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linear_search.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{}-[func]{linearSearchArray}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linear_search.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{linear_search_array}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linear_search.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{}-[func]{linearSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="linear_search.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{linearSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linear_search.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{linearSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linear_search.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{}-[func]{linearSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linear_search.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{linear_search}-[func]{linearSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linear_search.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{linearSearchArray}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linear_search.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{linearSearchArray}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
另一个例子,若需要在链表中查找给定目标节点值 `target` 并返回该节点对象,同样可以使用线性查找。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linear_search.java"
|
2023-02-07 19:05:19 +08:00
|
|
|
|
[class]{linear_search}-[func]{linearSearchLinkedList}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linear_search.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{}-[func]{linearSearchLinkedList}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linear_search.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{linear_search_linkedlist}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linear_search.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{}-[func]{linearSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="linear_search.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{linearSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linear_search.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{linearSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linear_search.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{}-[func]{linearSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linear_search.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{linear_search}-[func]{linearSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linear_search.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{linearSearchLinkedList}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linear_search.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{linearSearchLinkedList}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 复杂度分析
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**时间复杂度 $O(n)$** :其中 $n$ 代表数组或链表的长度。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**空间复杂度 $O(1)$** :无需借助额外的存储空间。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
## 优点与局限性
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**线性查找具有极佳的通用性**。由于线性查找是逐个访问元素的,没有跳跃式访问,因此适用于数组和链表的查找。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
**线性查找的时间复杂度较高**。当数据量 $n$ 较大时,线性查找的效率较低。
|