JS冒泡排序实例

15人浏览 / 0人评论
//冒泡排序
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));
document.write("输出结果:"+bubbleSort(arr)); 

全部评论