Keep the quick sort stack shallow.
transcript
Quick sort calls itself on both sides. A bad pivot can make a deep stack. How do you prevent that?
After a partition, there are two sides. Here the left side has two values. The right side has four. They can differ a lot in size.
Recurse into the smaller side. Then loop on the larger side. A loop uses no call stack.
The smaller side holds at most half of the range. So each open call halves the range. The depth is at most log base two of n.
Recursing both ways can nest a hundred thousand calls. With the smaller side first, a run needed only twelve. The bound is seventeen, since log of a hundred thousand is 16.6.
Compare the sizes of the two sides. Call the function on the smaller side. Then move lo or hi, to loop on the larger side.
Recurse into the smaller side first. Full breakdown in the lecture.