forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
338 changes: 122 additions & 216 deletions
docs/01_array/01_15_array_two_pointers.md
Oops, something went wrong.
190 changes: 97 additions & 93 deletions
docs/01_array/01_16_array_sliding_window.md
Oops, something went wrong.
391 changes: 173 additions & 218 deletions
docs/02_linked_list/02_01_linked_list_basic.md
Oops, something went wrong.
56 changes: 31 additions & 25 deletions
docs/02_linked_list/02_02_linked_list_sort.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 53 additions & 18 deletions
docs/02_linked_list/02_04_linked_list_selection_sort.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,81 @@ | ||
## 1. 链表选择排序算法描述 | ||
## 1. 链表选择排序算法思想 | ||
|
||
1. 使用两个指针 `node_i`、`node_j`。`node_i` 既可以用于控制外循环次数,又可以作为当前未排序链表的第一个链节点位置。 | ||
2. 使用 `min_node` 记录当前未排序链表中值最小的链节点。 | ||
3. 每一趟排序开始时,先令 `min_node = node_i`(即暂时假设链表中 `node_i` 节点为值最小的节点,经过比较后再确定最小值节点位置)。 | ||
4. 然后依次比较未排序链表中 `node_j.val` 与 `min_node.val` 的值大小。如果 `node_j.val < min_node.val`,则更新 `min_node` 为 `node_j`。 | ||
5. 这一趟排序结束时,未排序链表中最小值节点为 `min_node`,如果 `node_i != min_node`,则将 `node_i` 与 `min_node` 值进行交换。如果 `node_i == min_node`,则不用交换。 | ||
6. 排序结束后,继续向右移动 `node_i`,重复上述步骤,在剩余未排序链表中寻找最小的链节点,并与 `node_i` 进行比较和交换,直到 `node_i == None` 或者 `node_i.next == None` 时,停止排序。 | ||
7. 返回链表的头节点 `head`。 | ||
> **链表选择排序基本思想**: | ||
> | ||
> 在未排序部分中找到最小元素,然后将其放到已排序部分的末尾。 | ||
|
||
## 2. 链表选择排序实现代码 | ||
## 2. 链表选择排序算法步骤 | ||
|
||
1. **初始化**:使用两个指针 `node_i` 和 `node_j`。`node_i` 指向当前未排序部分的第一个节点,同时也用于控制外循环。 | ||
|
||
2. **寻找最小值**:在未排序部分中,使用 `min_node` 记录值最小的节点。初始时假设 `node_i` 为最小值节点。 | ||
|
||
3. **比较交换**:遍历未排序部分,比较每个节点的值。如果发现更小的值,则更新 `min_node`。 | ||
|
||
4. **交换操作**:一趟排序结束后,如果 `min_node` 不等于 `node_i`,则交换两个节点的值。 | ||
|
||
5. **移动指针**:将 `node_i` 向右移动一位,继续处理剩余未排序部分。 | ||
|
||
6. **终止条件**:当 `node_i` 为 `None` 或 `node_i.next` 为 `None` 时,排序完成。 | ||
|
||
## 3. 链表选择排序实现代码 | ||
|
||
```python | ||
class Solution: | ||
def sectionSort(self, head: ListNode): | ||
def selectionSort(self, head: ListNode): | ||
node_i = head | ||
# node_i 为当前未排序链表的第一个链节点 | ||
|
||
# 外层循环:遍历每个节点 | ||
while node_i and node_i.next: | ||
# min_node 为未排序链表中的值最小节点 | ||
# 假设当前节点为最小值节点 | ||
min_node = node_i | ||
node_j = node_i.next | ||
|
||
# 内层循环:在未排序部分寻找最小值 | ||
while node_j: | ||
if node_j.val < min_node.val: | ||
min_node = node_j | ||
node_j = node_j.next | ||
# 交换值最小节点与未排序链表中第一个节点的值 | ||
|
||
# 如果找到更小的值,则交换 | ||
if node_i != min_node: | ||
node_i.val, min_node.val = min_node.val, node_i.val | ||
|
||
# 移动到下一个节点 | ||
node_i = node_i.next | ||
|
||
return head | ||
|
||
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
return self.sectionSort(head) | ||
return self.selectionSort(head) | ||
``` | ||
|
||
## 3. 链表选择排序算法复杂度分析 | ||
## 4. 链表选择排序算法复杂度分析 | ||
|
||
| 指标 | 复杂度 | 说明 | | ||
|------|--------|------| | ||
| **最佳时间复杂度** | $O(n^2)$ | 无论初始顺序如何,都需 $O(n^2)$ 次比较 | | ||
| **最坏时间复杂度** | $O(n^2)$ | 无论初始顺序如何,都需 $O(n^2)$ 次比较 | | ||
| **平均时间复杂度** | $O(n^2)$ | 比较次数与数据状态无关 | | ||
| **空间复杂度** | $O(1)$ | 原地排序,仅使用常数额外空间 | | ||
| **稳定性** | ❌ 不稳定 | 可能改变相等节点的相对次序 | | ||
|
||
**适用场景**: | ||
|
||
- **小规模数据**:节点数量较少的场景(如 < 100) | ||
- **对空间复杂度严格**:仅使用常数额外空间的需求 | ||
- **交换代价高的场景**:选择排序交换次数少(最多 \(n-1\) 次) | ||
|
||
|
||
## 5. 总结 | ||
|
||
链表中的选择排序是一种简单直观的链表排序算法,通过在未排序部分中选择最小节点并将其放到已排序部分的末尾来完成排序。虽然实现简单,但效率较低。 | ||
|
||
- **时间复杂度**:$O(n^2)$。 | ||
- **空间复杂度**:$O(1)$。 | ||
**优点**:实现简单,空间复杂度低,交换次数少 | ||
**缺点**:时间复杂度高,不稳定,不适合大规模数据 | ||
|
||
## 练习题目 | ||
|
||
- [0148. 排序链表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/sort-list.md)(链表选择排序会超时,仅做练习) | ||
- [0148. 排序链表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/sort-list.md)(链表选择排序会超时,仅做练习) | ||
|
||
- [链表排序题目列表](https://github.com/ITCharge/AlgoNote/tree/main/docs/00_preface/00_06_categories_list.md#%E9%93%BE%E8%A1%A8%E6%8E%92%E5%BA%8F%E9%A2%98%E7%9B%AE) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.