首页 > 编程语言 > 详细

排序算法 | JS

时间:2021-07-22 11:12:39      阅读:14      评论:0      收藏:0      [点我收藏+]

冒泡排序:

Array.prototype.bubbleSort = function () {
  for(let i = 0; i < this.length - 1; i++){
    for(let j = 0; j < this.length - 1 -i;j += 1) {
      if(this[j] > this[j + 1]) {
        const temp = this[j];
        this[j] = this[j + 1];
        this[j + 1] = temp;
      }
    }
  }
};

const arr = [5, 4, 3, 2, 1];
arr.bubbleSort();
console.log(arr);

 

排序算法 | JS

原文:https://www.cnblogs.com/oaoa/p/15042226.html

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