Skip to content

Commit c4a6f11

Browse files
committed
修改插入排序算法 bug
1 parent ab5c5dc commit c4a6f11

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

‎notes/数据结构与算法.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ private static void swap(int[] arr, int i, int j) {
169169

170170
```java
171171
public static void sort(int[] arr) {
172-
for (int i = 0; i < arr.length; i++) {
173-
for (int j = i + 1; j >= 0; j--) {
172+
for (int i = 0; i < arr.length - 1; i++) {
173+
for (int j = i + 1; j > 0; j--) {
174174
if (arr[j] < arr[j - 1])
175175
swap(arr, j, j - 1); // 大量的交换会消耗时间
176176
else

0 commit comments

Comments
 (0)