1.给定一个数组,请你编写一个函数,返回该数组排序后的形式。
function MySort( arr ) {
if (arr.length <= 1) { return arr; }
let pivotIndex = Math.floor(arr.length / 2);//从arr中,从基准下标处删掉1个,也就是删掉基准,[0]表示取出删掉的第一个,赋值给pivot,因为不取出splice返回的是一个数组,
let pivot = arr.splice(pivotIndex, 1)[0];
let left = [];
let right = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i])
} else { right.push(arr[i])}
}
return MySort(left).concat([pivot],MySort(right))
}
module.exports = {
MySort : MySort
};