Professional Documents
Culture Documents
Time of Quicksort
Time of Quicksort
i n-i-1
First pointer
second pointer
My first pointer goes from a[1] to a[i] and compares the each
element with a[0]
Time for comparing all elements in a[1] to a[i] : i
Similarly,
My second pointer goes from a[n] to a[n-i-1] and compares
the each element with a[0]
Time for comparing all elements in a[1] to a[i] : n-i-1
In AVG. CASE
We take all possibilities
After ith iteration ,we assume that ith subarray is sorted
and rest of the subarrays and previous arrays are not sorted
T(n)/(n+1)−T(n−1)/(n)=2c/(n+1)
Expanding both of equation
T(n)/(n+1)+T(n−1)/(n)+T(n−2)/(n−1)+. . .+T(2)/(3)+T(1)/(2)
≈ 2c(log(n)) ≈ c′(log(n))