冒泡排序
冒泡排序(从大到小)的思想
拿数组的前一项跟后一项比较,若前一项小与后一项,则交换位置;
var arr = [1, 2, 45, 22, 8, 99, 21, 34, 56, 29];
var temp;
for (var i = 0; i <= arr.length - 2; i++) {//第0-arr.length-2个
for (var j = i + 1; j <= arr.length - 1; j++) { //第1-arr.length-1个
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
console.log(arr); //结果为[99, 56, 45, 34, 29, 22, 21, 8, 2, 1]