排序算法——冒泡排序

编程语言:JavaScript

算法核心思想:

		1.比较相邻的元素,如果第一个比第二个大,就交换他们两个        
		2.对每一对相邻元素做同样的工作
Array.prototype.bubbleSort = function () {                       
	 for(var i = 0; i < this.length; i ++) {
                for(var j = 0; j < this.length - 1 - i; j ++) {
                    //如果相邻元素。前一个比后一个大,则交换二者的位置 
                    if(this[j] > this[j + 1]) {                         
              	          //ES6语法 解构赋值             
                    	[this[j], this[j + 1]] = [this[j + 1], this[j]];                    
                    }
                }
            }
            return this;
        }

你可能感兴趣的:(算法)