Improved quicksort implementation