const quickSort1 = array => {
const left = []
const right = []
if(array.length <= 1) return array
const baseVal = array[array.length - 1]
for (let index = 0; index < array.length - 1; index++) {
if(baseVal > array[index]){
left.push(array[index])
}else{
right.push(array[index])
}
}
return [...quickSort(left),baseVal,...quickSort(right)]
}
function quickSort(arr,i,j){
var base = arr[i]
var start = i
var end = j
if( i < j) {
while( i < j){
while( i < j && arr[j] > base){
--j
}
arr[i] = arr[j]
while( i < j && arr[i] < base){
++i
}
arr[j] = arr[i]
}
arr[i] = base
quickSort(arr, start, i -1)
quickSort(arr, i + 1, end)
}
return arr
}
var arr = [85, 24, 63, 45, 17, 31, 96, 50]
arr = quickSort(arr,0,arr.length - 1)
console.log(arr)
网友评论