Worst-case running time of algorithm

In summary, the conversation discusses the worst-case running time of Quicksort, an algorithm used for sorting elements in a sequence. It is shown that the best partitioning algorithm for Quicksort has a time complexity of $\Theta(n)$, but in the worst case scenario where the pivot element is always the smallest, the total time complexity becomes $\Theta(n^2)$. The need for further justification, such as a recurrence relation, is also mentioned.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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
  • #2
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)$$
 
  • #3
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)
 

Related to Worst-case running time of algorithm

What is the worst-case running time of an algorithm?

The worst-case running time of an algorithm refers to the maximum amount of time it would take for the algorithm to complete its task, assuming the input is the most difficult or challenging for the algorithm to handle. It is used to measure the efficiency of an algorithm and to determine how scalable it is.

How is the worst-case running time determined?

The worst-case running time is determined by analyzing the algorithm and identifying the operations that take the longest to execute. These operations are then counted and the number is used to estimate the worst-case running time.

Why is the worst-case running time important?

The worst-case running time is important because it helps us understand how an algorithm will perform in the worst-case scenario. This is important for real-world applications where the input data may not always be in the optimal format. It also allows us to compare the efficiency of different algorithms and choose the most efficient one for a given task.

Can the worst-case running time be improved?

In most cases, the worst-case running time cannot be improved, as it is a characteristic of the algorithm itself. However, it is possible to improve the average or best-case running time of an algorithm through optimization techniques such as reducing the number of operations or using more efficient data structures.

How can the worst-case running time be reduced?

The worst-case running time can be reduced by choosing a different algorithm that is more efficient or by analyzing the existing algorithm and making improvements to optimize its performance. This may require making trade-offs between time and space complexity, as reducing one may result in an increase in the other.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
30
Views
4K
Back
Top