//冒泡排序
function bubbleSort(myArray) { if (myArray.length == 0) return [] for (var i = 0; i < myArray.length; i++) { for (var j = i + 1; j < myArray.length; j++) { var myArrayI = myArray[i]; var myArrayJ = myArray[j]; if (myArrayI > myArrayJ) { myArray[i] = myArrayJ; myArray[j] = myArrayI; } } } return myArray } var arr = [52, 1, 2, 7, 3, 2, 5, 78, 485, 33, 2]; console.log(bubbleSort(arr));
输出结果: arr=[1,2,2,2,3,5,7,33,52,78,485]