Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why the method quicksort takes longer to sort a vector is a vector ordered than disordered?
Posted

Quicksort is faster in most cases, except when the vector (in that case) is already sorted.

In the worse case it is O(n2), on average, it is O(n log n) which is what we usually look for when describing algorithms, we look for the best average "complexity" speed, not the worse cases.

If I remember correctly, when an array is sorted, a simple bubble sort will be faster than QuickSort.
 
Share this answer
 
Because you used the vector ordered the way that the number of sorting operations is more than in an average case of randomly ordered vector. I don't see why it can come at surprise.

—SA
 
Share this answer
 
Comments
Nelek 26-Jun-13 10:43am    
Maybe he thought: If already sorted, it should not take so long because it is not needed to move anything.
Sergey Alexandrovich Kryukov 26-Jun-13 11:03am    
Yo are right of course, but, for further investigation, one would need an example of sorting: nothing tells me that it's sorted in the required order, it could be some other order (say, reverse). Besides, sorted vector still needs a checkup that it is properly sorted. The sorting algorithm is optimized for sorting starting from the random order.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900