MHB Worst-case running time of algorithm

AI Thread Summary
The discussion focuses on demonstrating that the worst-case running time of the Quicksort algorithm is O(n^2). The algorithm involves selecting a random pivot and partitioning the array into three segments: elements less than, equal to, and greater than the pivot. The worst-case scenario occurs when the pivot is consistently the smallest element, leading to an unbalanced partition. This results in a series of recursive calls that can be expressed as a sum of the form n + Θ(n) + Θ(n-1) + ... + Θ(1), which simplifies to Θ(n^2). The question raised is whether this explanation suffices or if further justification, such as using a recurrence relation, is necessary to support the claim of O(n^2) complexity.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

Let the quicksort algorithm be the following:

Code:
procedure QUICKSORT(S)
         if S contains at no one element then return S
         else
            begin
               choose an element a randomly from S;
               let S1, S2 and S3 be the sequences of elements in S less than, equal to and greater than a, respectively;
               return (QUICKSORT(S1) followed by S2 followed by QUICKSORT(S3))
            end

How can I show that the worst-case running time of Quicksort is $O(n^2)$ ?? (Wondering)
 
Technology news on Phys.org
choose an element a randomly from S; let S1, S2 and S3 be the sequences
of elements in S less than, equal to
and greater than a, respectively;

The above is a description of a partitioning algorithm. It can be shown that the best partitioning algorithm has order $\Theta (n)$ where n is the number of elements of S. Suppose all elements of S are different and at each call to partition (unluckily) the pivot a is the smallest element. Then the total order of quicksort is $$n+\Theta (n)+\Theta(n-1)+\cdots +\Theta(1)=\Theta(n^2)$$
 
So, to show that the worst-case running time of Quicksort is $O(n^2)$ do we have to say just the following:

johng said:
Suppose all elements of S are different and at each call to partition (unluckily) the pivot a is the smallest element. Then the total order of quicksort is $$n+\Theta (n)+\Theta(n-1)+\cdots +\Theta(1)=\Theta(n^2)$$

?? (Wondering)

Or do we have to justify it further, for example with a recurrence relation?? (Wondering)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top