1 冒泡排序O(n2)
规则:前后两两进行比较,如果符合比较的条件,就交换两个数的位置,直到所有的数据,都符合从小到大的条件,排序结束。
规律:每一轮都会找出一个较大的数,放在正确的位置。
比较的轮数:数组长度-1;
每一轮比较的次数:数组长度-当前轮数;
function buluBulu(arr = []) { const len = arr?.length; for (let i = 0;i<len-1;i++){ //需要比较几轮arr.length-1 for (let j= 0 ;j<len-i-1;i++){ //每一轮的比较(arr.length-i次数)找出一个最大值, if (arr[j]>arr[j+1]){ [arr[j],arr[j+1]] = [arr[j+1],arr[j]] } } }
return arr;
}