functionqsort(array, l, r){//快排O(NlogN)if(l >= r){return}var key = array[l]var i = l
var j = r
while(i < j){while(i < j && array[j]>= key){
j--}
array[i]= array[j]while(i < j && array[i]<= key){
i++}
array[j]= array[i]}
array[i]= key
qsort(array, l, i -1)qsort(array, i +1, r)}
functionqsort(arr){//快排//方法2if(arr.length <2){return arr
}// var center = arr.splice(parseInt(arr.length / 2), 1)[0]var center = arr[0]var left =[]var right =[]for(var i =1; i < arr.length; i++){if(arr[i]< center){
left.push(arr[i])}else{
right.push(arr[i])}}return[...qsort1(left), center,...qsort1(right)]}
functionselectSort(array){//选择排序O(N2)for(var i =0; i < array.length -1; i++){var minIndex = i
for(var j = i +1; j < array.length; j++){if(array[j]< array[minIndex]){
minIndex = j
}}var tmp = array[minIndex]
array[minIndex]= array[i]
array[i]= tmp
}}
functionbubblingSort(array){//冒泡排序O(N2)//外层循环走趟数=length-1 内层循环走次数length-i-1for(var i =0; i <= array.length -1; i++){var flag =1for(var j =0; j < array.length - i -1; j++){if(array[j]> array[j +1]){var temp = array[j];
array[j]= array[j +1];
array[j +1]= temp;
flag =0}}if(flag ===1){break}}}
functioninsertSort(arr){//插入排序for(var i =1; i < arr.length; i++){var t = arr[i]var j = i -1while(t < arr[j]&& j >=0){
arr[j +1]= arr[j]
j--}
arr[j +1]= t
}}