Skip to content
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

插入排序有错误 #31

Open
XJ-Up opened this issue Dec 7, 2020 · 0 comments
Open

插入排序有错误 #31

XJ-Up opened this issue Dec 7, 2020 · 0 comments

Comments

@XJ-Up
Copy link

XJ-Up commented Dec 7, 2020

“j”的取值范围应该 “j>=0”
public void insertSort(int[] arr) {
int len = arr.length;
//要插入的数
int insertNum;
//因为第一次不用,所以从1开始
for (int i = 1; i < len; i++) {
insertNum = arr[i];
//序列元素个数
int j = i - 1;
//从后往前循环,将大于insertNum的数向后移动
while (j >= 0 && arr[j] > insertNum) {
//元素向后移动
arr[j + 1] = arr[j];
j--;
}
//找到位置,插入当前元素
arr[j + 1] = insertNum;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant