Skip to content

插入排序有错误 #31

Description

@XJ-Up

“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;
}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions