function bubbleSort(arr) {
for(var i = 1; i < arr.length; i++) {
for (var j = 0; j < arr.length-i; j++) {
if(arr[j] > arr[j+1]) {
var temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
console.log(bubbleSort(arr));
function bubbleSort(arr) {
for(var i = 1; i < arr.length; i++) {
for (var j = 0; j < arr.length-i; j++) {
if(arr[j] > arr[j+1]) {
var temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
console.log(bubbleSort(arr));
本文标题:冒泡排序
本文链接:https://www.haomeiwen.com/subject/gzufbqtx.html
网友评论