1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| const quickSort = (function () { function partition(arr, start, end) { let v = arr[start] let i = start + 1 let j = end
while (i < j) { while (arr[i] <= v && i <= j) { i++ } while (arr[j] >= v && j >= i) { j-- } if (i < j) { [arr[j], arr[i]] = [arr[i], arr[j]] } }
if (v > arr[j]) { [arr[start], arr[j]] = [arr[j], v] }
return j } function quickSortEx(arr, start, end) { if (start < end) { let pivotPos = partition(arr, start, end) quickSortEx(arr, start, pivotPos - 1) quickSortEx(arr, pivotPos + 1, end) } } return function quickSort(arr) { quickSortEx(arr, 0, arr.length - 1) return arr } })() quickSort([4, 5, 6, 3, 2, 1, 8, 9, 7])
|