Optimize quick sort for duplicates