Problems178
Search for a command to run...
Try your own input
Change the array and replay the algorithm from step one.
Current step
Quick Sort: Pick a pivot and place it in its correct position. Move smaller elements to the left and larger to the right.
Quick Sort
1class Solution {2 partition(arr, low, high) {3 let pivot = arr[low];4 let i = low;5 let j = high;6 7 while (i < j) {8 while (arr[i] <= pivot && i <= high - 1) {9 i++;10 }11 while (arr[j] > pivot && j >= low + 1) {12 j--;13 }14 if (i < j) {15 [arr[i], arr[j]] = [arr[j], arr[i]];16 }17 }18 19 [arr[low], arr[j]] = [arr[j], arr[low]];20 return j;21 }22 23 quickSortHelper(arr, low, high) {24 if (low < high) {25 let pIndex = this.partition(arr, low, high);26 this.quickSortHelper(arr, low, pIndex - 1);27 this.quickSortHelper(arr, pIndex + 1, high);28 }29 }30 31 quickSort(nums) {32 this.quickSortHelper(nums, 0, nums.length - 1);33 return nums;34 }35}