Question

In: Computer Science

Derive the recurrence for the average time complexity of Quick Sort.

Derive the recurrence for the average time complexity of Quick Sort.

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

The average case run time of Quick sort is O(nlogn).

  • Assume a strategy for choosing pivots such that, after partitioning A into A1 and A2, all lengths of A1 from 0 to |A|-1 are equally likely.

  • The running time of quickSort equals the time of its two recursive calls plus time to do the partition.

    • pivot index is O(last-first).

    Suppose that we partition n elements into sub-arrays of length i and (n-i).

Time T to sort the n elements is then:

T(n)=T(i)+T(n-i)+c⋅n

  • This kind of formula is called a recurrence relation. They are very common in describing the performance of recursive routines.

Because i is equally likely to be any value from 0 to n-1, the average (expected) value of T(i) is

E(T(i))=1n∑j=0n-1T(j)

Since n-i can take on the same values as i, and all such values are equally likely,

E(T(n-i))=E(T(i))

On average, then

T(n)=2n(∑j=0n-1T(j))+c⋅n

Multiply through by n:

n⋅T(n)=2⋅(∑j=0n-1T(j))+c⋅n2

n is just a variable, so this must be true no matter what value we plug in for it. Try replacing n by n-1.

(n-1)⋅T(n-1)=2⋅(∑j=0n-2T(j))+c⋅(n-1)2

So now both of these are true:

n⋅T(n)=2⋅(∑j=0n-1T(j))+c⋅n2(n-1)⋅T(n-1)=2⋅(∑j=0n-2T(j))+c⋅(n-1)2

Subtract the 2nd equation from the first:

nT(n)-(n-1)T(n-1)=2T(n-1)+2cn-c

Collect the T(n-1) terms together and drop the -c term:

nT(n)=(n+1)T(n-1)+2cn

Next we apply a standard technique for solving recurrence relations. Divide by n(n+1) and "telescope":

T(n)n+1=T(n-1)n+2cn+1T(n-1)n=T(n-2)n-1+2cnT(n-2)n-1=T(n-3)n-2+2cn-1⋮

Note that most of the terms on the left will have appeared on the right in the previous equation, so if we were to add up all these equations, these terms would appear on both sides and could be dropped:

T(n)n+1=T(1)2+2c∑j=3n+11j

As n gets very large, ∑j=3n+11j approaches ln(n)+γ, where γ is Euler's constant, 0.577...

Hence

T(n)n+1=T(1)2+2c⋅ln(n)+2cγ=ln(n)+c2=O(ln(n))

and so

T(n)=O(n⋅log(n))

Kindly revert for any queries

Thanks.


Related Solutions

a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement in the algorithm. (You must explain your reason for each statement about how you get the answer of each computation time in at one or two sentences each.)
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm....
1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm. T(n) = T(n-1)+1 , if n> 0 1 if n = 0
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN)...
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN) B. Evaluate the following postfix expression. 2 3 4 + * C. In an array representation of heap, what are the parent node of a node a[10]? a[9] a[11] a[5] a[20] There is no easy way to access parent node in such representation. D. In an array representation of heap, what are the children nodes (if any) of a node a[10]? a[11] and a[12]...
Analyze the time complexity of the following variant of merge sort: given a constant k, divide...
Analyze the time complexity of the following variant of merge sort: given a constant k, divide the array into k parts, sort each part recursively, and merge the results.
What is the runtime for quick sort?
What is the runtime for quick sort?
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAM PS: YOU ARE ANSEWRING THE SAME PROGRAMS I WANT DIFFERENT ONE PLEASE , THANK YOU . BECAUSE THE ONE WERE POSTING DOESNT WORKING !!
What is the Average time complexity of sequential search algorithm in a linked list?
What is the Average time complexity of sequential search algorithm in a linked list?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT