快排
function getMid(arr, left, right) { let mid = arr[left]; while(left<right) { while(left<right && arr[right]>=mid) right--; let temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; while(left<right && arr[left]<=mid) left++; temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } return left; } function sort(arr, left, right){ if(left<right) { let mid = getMid(arr, left, right); sort(arr, left, mid-1); sort(arr, mid+1, right); } } let arr = [3, 6, 8, 10, 1, 2, 1]; sort(arr, 0, arr.length - 1); console.log(arr)
垃圾快排 三次面试被问然后挂了