1.冒泡排序(相邻2个元素依次比较换位)
let arr=[2,1,3,6,4,5,8,9,2,6,12,45,89,34]; function fn(arr){ if(arr.length>0){ for(let i=0;i<arr.length-1;i++){ for(let j=0;j<arr.length-i-1;j++){ if(arr[j]>arr[j+1]){ let temp=''; temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] =temp; } } } return new Set(arr); }else{ return []; } } fn(arr)
2.快速排序(取中间元素,小于中间元素放数组左边,大于中间元素放数组右边)
let arr=[2,1,3,6,4,5,8,9,2,6,12,45,89,34]; function fn(arr){ if(arr.length>0){ let c=Math.floor(arr.length/2); let data=arr.splice(c,1); let left=[],right=[]; for(let i=0;i<arr.length;i++){ arr[i]<data?left.push(arr[i]):right.push(arr[i]); } return fn(left).concat(data,fn(right)); }else{ return []; } } new Set(fn(arr))