I am unable to understand why unbalanced partitions in quicksort is actually worse than balanced partitions.
After reading this document it shows that worse case partitions are of the type $(0,(n-1)), (0,(n-2)), (0,(n-3))$ and so on. So the work on every step is $Cn, C(n-1), C(n-2)$ , it actually decreases since $C(n) > C(n-1)$. Correct me if I am wrong.
While in best case the partitions on every step can be assumed as $n/2$ on each side or $n/2$ or $n/2 - 1$ on each side. So the work done on each step, is $Cn$ always.
So how is that better than unbalanced partitions ? The work actually decreases in every step for unbalanced partitions, while for balanced partitions the work actually remains constant on all the steps and depending on the partition algo it may even include the pivot which is already sorted.
4,5,3,2,1
. If 3 is selected as the pivot and (Hoare's original partition) [https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme] scheme is used theni
andj
meet at the same point. Then the partition becomes1,2,3
and4,5
. The pivot is still there in one of the partition. Ideally the resultant partitions should be1,2
and4,5
– ng.newbie Aug 16 '17 at 10:53