Implementing and improving a sorting routine
Question
Code up a sorting algorithm of your choice, analyse its running time and memory use, and then suggest improvements.
Short answer
A good choice is quicksort: average time, stack, but worst case. Partition around a pivot and recurse on both sides. Improve with median-of-three or random pivots, three-way partitioning for duplicates, insertion sort on small subarrays, recursing on the smaller side first, and falling back to heapsort when depth grows (introsort). Merge sort guarantees and stability at the price of extra memory.
Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.17, p.226; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solution