Closed HTML-Worker closed 5 years ago
@HTML-Worker for循环少减少了一个i。修改的地方 for(var j = 0; j < arr.length - i - 1; j++) {
var arr = [3, 1, 4, 6, 5, 7, 2];
function bubbleSort(arr) {
for (var i = 0; i < arr.length - 1; i++) {
for(var j = 0; j < arr.length - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
var temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
console.log(bubbleSort(arr));
应该是 if(flag){ break; }