Given an array of integers nums, sort the array in ascending order using the Quick Sort algorithm — pick a pivot element, partition the array so that all elements smaller than the pivot land to its left and all elements greater land to its right, then recursively sort each side.
nums = [8,4,3,7,6][3,4,6,7,8]After a partition step completes, the pivot is guaranteed to sit in its final sorted position — every element to its left is ≤ the pivot and every element to its right is ≥ the pivot. That means recursion never needs to revisit that index again; each recursive call only has to solve the strictly smaller left and right subranges, until ranges shrink to 0 or 1 elements (already sorted by definition).
Simple sorts compare almost every pair, which is O(n²). Quick sort divides the problem instead.
Pick a pivot, here the last element. Partition: move everything smaller than the pivot to the left side. Then put the pivot right after them, which is its final sorted position. Now sort the left part and the right part recursively. Each partition is one linear scan.
Partition puts the pivot in its final place. Choose pivots well to avoid the quadratic worst case.
Run the animation above on your own input, or watch the 1:48 video explanation of Quick Sort with a dry run and Java code.