function bubbleSort(arr) { if (arr.length <= 0) { return arr; } for (var i = 0, len = arr.length - 1; i < len; i++) { for (var j = 0; j < len - 1- i; j++) { if (arr[j] > arr[j + 1]) { var temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } return arr; } var arr = [1,34 ,3 , 5, 77, 2, 89]; console.log(bubbleSort(arr)); // [ 1, 2, 3, 5, 34, 77, 89 ]
时间: 2024-10-09 19:23:23