AndroidNote
AndroidNote copied to clipboard
插入排序有错误
“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; } }