arr.splice()剪接 API注释:Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. 从数组中删除元素,并在必要时在其位置插入新元素,返回已删除的元素。
concat
this.quickSort(left).concat([pivot], this.quickSort(right));
Combines two or more arrays. API注释:组合两个或多个数组
//快速排序
public static quickSort(arr:number[]):number[]{
if(arr.length <= 1){
return arr;
}
var pivotIndex = Math.floor(arr.length/2);
var pivot = arr.splice(pivotIndex,1)[0];
var left = [] as number[];
var right = [] as number[];
for(var i =0;i<arr.length;i++){
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return this.quickSort(left).concat([pivot], this.quickSort(right));//自己调用自己 把左,中间,右三个数组连接