Being the recursion depth the maximum number of successive recursive calls before QuickSort hits it´s base case, and noting that it (recursion depth) is a random variable, since it depends on the chosen pivot.
What I want is to estimate the minimum-possible and maximum-possible recursion depth of QuickSort.
The following procedure describes the way thats QuickSort is normally implemented:
QUICKSORT(A,p,r)
if p<r
q ← PARTITION(A,p,r)
QUICKSORT(A,p,q−1)
QUICKSORT(A,q+1,r)
return A
PARTITION(A,p,r)
x←A[r]
i←p−1
for j ←p to r−1
if A[j] ≤ x
i ← i +1
exchange A[i] ↔ A[j]
exchange A[i +1] ↔ A[r]
return i +1
The second recursive call in QuickSort is not really necessary; it can be avoided by using an iterative control structure. This technique is also called tail recursion, and it can be implemented like:
QUICKSORT_tail(A,p,r)
while p<r
q ← PARTITION(A,p,r)
QUICKSORT(A,p,q−1)
p ← q+1
return A
In this version, the information for the most recent call is at the top of the stack, and the information for the initial call is at the bottom. When a procedure is invoked, its information is pushed onto the stack; when it terminates, its information is popped. Since I assume that array parameters are represented by pointers, the information for each procedure call on the stack requires O(1) stack space. I also believe that the maximum-possible stack space with this version should be θ(n).
So, after all this said, how can I estimate the minimum-possible and maximum-possible recursion depth of each QuickSort version? Am I right in the above inference?
Thanks in advance.