Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] main from itcharge:main #28

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
pull merged 5 commits into AlgorithmAndLeetCode:main from itcharge:main
Aug 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update 03.Array-Insertion-Sort.md
  • Loading branch information
itcharge committed Aug 16, 2022
commit 5ee02b34f58860b2cbab982da59633abdb436bec
22 changes: 16 additions & 6 deletions Contents/01.Array/02.Array-Sort/03.Array-Insertion-Sort.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
1. 第 `1` 趟排序:
1. 第 `1` 个元素为有序序列,后面第 `2` ~ `n `个元素(总共 `n - 1` 个元素)为无序序列。
2. 从右至左遍历有序序列中的元素,如果遇到「有序序列的元素 > 无序序列的第 `1` 个元素」的情况时,则将向有序序列的元素后移动一位。
3. 如果遇到「有序序列的元素 <= 无序序列的第 `1` 个元素」的情况时,则说明找到了插入位置。将「无序序列的第 `1` 个元素」插入该位置。
3. 如果遇到「有序序列的元素 <= 无序序列的第 `1` 个元素」的情况或者「到达数组开始位置」时,则说明找到了插入位置。将「无序序列的第 `1` 个元素」插入该位置。
2. 第 `2` 趟排序:
1. 第 `1` ~ `2` 个元素为有序序列,后面第 `3` ~ `n` 个元素(总共 `n - 2` 个元素)为无序序列。
2. 从右至左遍历有序序列中的元素,如果遇到「有序序列的元素 > 无序序列的第 `1` 个元素」的情况时,则将向有序序列的元素后移动一位。
3. 如果遇到「有序序列的元素 <= 无序序列的第 `1` 个元素」的情况时,则说明找到了插入位置。将「无序序列的第 `1` 个元素」插入该位置。
3. 如果遇到「有序序列的元素 <= 无序序列的第 `1` 个元素」的情况或者「到达数组开始位置」时,则说明找到了插入位置。将「无序序列的第 `1` 个元素」插入该位置。

3. 依次类推,对剩余 `n - 3` 个元素重复上述排序过程,直到所有元素都变为有序序列,则排序结束。

Expand All @@ -29,7 +29,16 @@

## 3. 插入排序动画演示

![img](https://www.runoob.com/wp-content/uploads/2019/03/insertionSort.gif)
![](https://qcdn.itcharge.cn/images/20220816143518.gif)

1. 初始序列为:`[6, 2, 3, 5, 1, 4]`。
2. 第 `1` 趟排序,将 `[6]` 作为有序序列,把 `[2, 3, 5, 1, 4]` 作为无序序列。无序序列第 `1` 个元素为 `2`。
1. 从右向左遍历有序序列 `[6]`,遇到 `6 > 2`,则将 `6` 向右移动 `1` 位,到达数组开始位置,则找到了合适的插入位置,将 `2` 插入该位置。
2. 此时序列变为 `[2, 6, 3, 5, 1, 4]`。
3. 第 `2` 趟排序,把 `[2, 6]` 作为有序序列, `[3, 5, 1, 4]` 为无序序列。无序序列第 `1` 个元素为 `3`。
1. 从右向左遍历有序序列 `[6]`,遇到 `6 > 3`,则将 `6` 向右移动 `1` 位,继续遍历,遇到 `2 < 3`,则找到了合适的插入位置,将 `3` 插入该位置。
2. 此时序列变为 `[2, 3, 6, 5, 1, 4]`。
4. 依次类推,对无序序列中剩余 `3` 个元素重复上述排序过程,直到无序序列中所有元素都插入到有序序列,则排序结束。此时,序列变为 `[1, 2, 3, 4, 5, 6]`。

## 4. 插入排序算法分析

Expand All @@ -43,19 +52,20 @@
```Python
class Solution:
def insertionSort(self, arr):
# 遍历无序序列
for i in range(1, len(arr)):
temp = arr[i]
j = i
# 从右至左遍历有序序列
while j > 0 and arr[j - 1] > temp:
# 将有序序列中插入位置右侧的元素依次右移一位
arr[j] = arr[j - 1]
j -= 1
# 将该元素插入到适当位置
arr[j] = temp

return arr

def sortArray(self, nums: List[int]) -> List[int]:
return self.insertionSort(nums)
```



AltStyle によって変換されたページ (->オリジナル) /