this is a homework question, and I’m not that at finding the complixity but I’m trying my best!
- Three-way partitioning is a modification of quicksort that partitions elements into groups smaller than, equal to, and larger than the pivot. Only the groups of smaller and larger elements need to be recursively sorted. Show that if there are N items but only k unique values (in other words there are many duplicates), then the running time of this modification to quicksort is O(Nk).
my try:
on the average case:
the tree subroutines will be at these indices:
I assume that the subroutine that have duplicated items will equal (n-k)
- first: from 0 – to(i-1)
- Second: i – (i+(n-k-1))
- third: (i+n-k) – (n-1)
- number of comparisons = (n-k)-1
So,
T(n) = (n-k)-1 + Sigma from 0 until (n-k-1) [ T(i) + T (i-k)]
then I’m not sure how I’m gonna continue :S
It might be a very bad start though :$
Hope to find a help
First of all, you shouldn’t look at the average case since the upper bound of
O(nk)can be proved for the worst case, which is a stronger statement.You should look at the maximum possible depth of recursion. In normal quicksort, the maximum depth is
n. For each level, the total number of operations done isO(n), which givesO(n^2)total in the worst case.Here, it’s not hard to prove that the maximum possible depth is
k(since one unique value will be removed at each level), which leads toO(nk)total.