This recent patch:
Added quick_sort_locations(). This replaces some O(n^2) algorithms with some that are O(n log(n))
Tries to optimise an O(n^2) operation to O(n log(n)), yet uses quicksort in the process which can be O(n^2) all by itself. Why not use (my personal favourite): heapsort? Heapsort is O(n log(n)) as the *worst* case.