首页 > 编程语言 > 详细

js冒泡排序法-(从大到小排序,从小到大排序)

时间:2020-06-02 13:26:44      阅读:269      评论:0      收藏:0      [点我收藏+]
// 冒泡排序法-从大到小排序
let itemSort = [8, 1, 4, 7, 3, 2, 9];
for (let i = 0; i < itemSort.length; i++) {
    for (let y = i; y > 0; y--) {
        if (itemSort[y] > itemSort[y - 1]) {
            [itemSort[y - 1], itemSort[y]] = [itemSort[y], itemSort[y - 1]]
        }
    }
}
// 冒泡排序法-从小到大排序
let itemSort = [8, 1, 4, 7, 3, 2, 9];
for (let i = 0; i < itemSort.length; i++) {
    for (let y = i; y > 0; y--) {
        if (itemSort[y] < itemSort[y - 1]) {
            [itemSort[y - 1], itemSort[y]] = [itemSort[y], itemSort[y - 1]]
        }
    }
}
console.log(itemSort)

 

js冒泡排序法-(从大到小排序,从小到大排序)

原文:https://www.cnblogs.com/jwzhang/p/13030128.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!