2022-11-22 17:47:26 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-01-31 03:37:50 +08:00
|
|
|
|
# 10.1. 线性查找
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
「线性查找 Linear Search」是一种最基础的查找方法,其从数据结构的一端开始,依次访问每个元素,直到另一端后停止。
|
|
|
|
|
|
2023-01-31 03:37:50 +08:00
|
|
|
|
## 10.1.1. 算法实现
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
线性查找实质上就是遍历数据结构 + 判断条件。比如,我们想要在数组 `nums` 中查找目标元素 `target` 的对应索引,那么可以在数组中进行线性查找。
|
|
|
|
|
|
|
|
|
|
![linear_search](linear_search.assets/linear_search.png)
|
|
|
|
|
|
|
|
|
|
=== "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"
|
2022-12-17 15:14:38 +08:00
|
|
|
|
/* 线性查找(数组) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
func linearSearchArray(nums []int, target int) int {
|
2022-12-17 15:14:38 +08:00
|
|
|
|
// 遍历数组
|
|
|
|
|
for i := 0; i < len(nums); i++ {
|
|
|
|
|
// 找到目标元素,返回其索引
|
|
|
|
|
if nums[i] == target {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标元素,返回 -1
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="linear_search.js"
|
2022-12-22 18:48:18 +08:00
|
|
|
|
/* 线性查找(数组) */
|
|
|
|
|
function linearSearchArray(nums, target) {
|
|
|
|
|
// 遍历数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
// 找到目标元素,返回其索引
|
|
|
|
|
if (nums[i] === target) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标元素,返回 -1
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linear_search.ts"
|
2023-01-07 13:41:46 +08:00
|
|
|
|
/* 线性查找(数组)*/
|
|
|
|
|
function linearSearchArray(nums: number[], target: number): number {
|
|
|
|
|
// 遍历数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
// 找到目标元素,返回其索引
|
|
|
|
|
if (nums[i] === target) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标元素,返回 -1
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linear_search.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linear_search.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 线性查找(数组) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
int linearSearchArray(int[] nums, int target)
|
2022-12-23 15:42:02 +08:00
|
|
|
|
{
|
|
|
|
|
// 遍历数组
|
|
|
|
|
for (int i = 0; i < nums.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
// 找到目标元素,返回其索引
|
|
|
|
|
if (nums[i] == target)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标元素,返回 -1
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linear_search.swift"
|
2023-01-30 15:43:29 +08:00
|
|
|
|
/* 线性查找(数组) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
func linearSearchArray(nums: [Int], target: Int) -> Int {
|
2023-01-30 15:43:29 +08:00
|
|
|
|
// 遍历数组
|
|
|
|
|
for i in nums.indices {
|
|
|
|
|
// 找到目标元素,返回其索引
|
|
|
|
|
if nums[i] == target {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标元素,返回 -1
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linear_search.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
再比如,我们想要在给定一个目标结点值 `target` ,返回此结点对象,也可以在链表中进行线性查找。
|
|
|
|
|
|
|
|
|
|
=== "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"
|
2022-12-17 15:14:38 +08:00
|
|
|
|
/* 线性查找(链表)*/
|
|
|
|
|
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
|
|
|
|
|
// 遍历链表
|
|
|
|
|
for node != nil {
|
|
|
|
|
// 找到目标结点,返回之
|
|
|
|
|
if node.Val == target {
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
node = node.Next
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标元素,返回 nil
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="linear_search.js"
|
2022-12-22 18:48:18 +08:00
|
|
|
|
/* 线性查找(链表)*/
|
|
|
|
|
function linearSearchLinkedList(head, target) {
|
|
|
|
|
// 遍历链表
|
|
|
|
|
while(head) {
|
|
|
|
|
// 找到目标结点,返回之
|
|
|
|
|
if(head.val === target) {
|
|
|
|
|
return head;
|
|
|
|
|
}
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标结点,返回 null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linear_search.ts"
|
2023-01-07 13:41:46 +08:00
|
|
|
|
/* 线性查找(链表)*/
|
|
|
|
|
function linearSearchLinkedList(head: ListNode | null, target: number): ListNode | null {
|
|
|
|
|
// 遍历链表
|
|
|
|
|
while (head) {
|
|
|
|
|
// 找到目标结点,返回之
|
|
|
|
|
if (head.val === target) {
|
|
|
|
|
return head;
|
|
|
|
|
}
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标结点,返回 null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linear_search.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linear_search.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 线性查找(链表) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
ListNode? linearSearchLinkedList(ListNode head, int target)
|
2022-12-23 15:42:02 +08:00
|
|
|
|
{
|
|
|
|
|
// 遍历链表
|
|
|
|
|
while (head != null)
|
|
|
|
|
{
|
|
|
|
|
// 找到目标结点,返回之
|
|
|
|
|
if (head.val == target)
|
|
|
|
|
return head;
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标结点,返回 null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linear_search.swift"
|
2023-01-30 15:43:29 +08:00
|
|
|
|
/* 线性查找(链表) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
func linearSearchLinkedList(head: ListNode?, target: Int) -> ListNode? {
|
2023-01-30 15:43:29 +08:00
|
|
|
|
var head = head
|
|
|
|
|
// 遍历链表
|
|
|
|
|
while head != nil {
|
|
|
|
|
// 找到目标结点,返回之
|
|
|
|
|
if head?.val == target {
|
|
|
|
|
return head
|
|
|
|
|
}
|
|
|
|
|
head = head?.next
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标结点,返回 null
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linear_search.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-31 03:37:50 +08:00
|
|
|
|
## 10.1.2. 复杂度分析
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-09 22:39:30 +08:00
|
|
|
|
**时间复杂度 $O(n)$** :其中 $n$ 为数组或链表长度。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-09 22:39:30 +08:00
|
|
|
|
**空间复杂度 $O(1)$** :无需使用额外空间。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-31 03:37:50 +08:00
|
|
|
|
## 10.1.3. 优点与缺点
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**线性查找的通用性极佳**。由于线性查找是依次访问元素的,即没有跳跃访问元素,因此数组或链表皆适用。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**线性查找的时间复杂度太高**。在数据量 $n$ 很大时,查找效率很低。
|