1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| /** * 冒泡排序基础 * arry:数组,必传 * type:排序方式,默认值1,1:升序,0:降序 **/ function bubbleSort(arry,type = 1){ if(Object.prototype.toString.call(arry) !== '[object Array]'){//判断是否数组 return false; } for (let i=0;i<arry.length;i++){ for(let j=0;j<arry.length-i;j++){ if((type == 1 && arry[j]>arry[j+1])||(type == 0 && arry[j]<arry[j+1])){ let temp = arry[j]; arry[j] = arry[j+1]; arry[j+1] = temp; } } } return arry; }
|