Execution Time of QuickSort for Different Inputs

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Function Time
Click For Summary
SUMMARY

The discussion focuses on the execution time of the QuickSort algorithm under different input conditions, specifically when all elements are identical and when elements are sorted in decreasing order. It establishes that the partition function returns the index of the last element when all elements are the same, leading to a worst-case execution time of QuickSort as Θ(n²) for sorted inputs. Conversely, the best-case scenario for QuickSort with distinct elements is O(n log n). The pseudocode provided illustrates the recursive nature of QuickSort and the partitioning process.

PREREQUISITES
  • Understanding of QuickSort algorithm and its pseudocode
  • Familiarity with algorithmic time complexity notation (Θ, O)
  • Knowledge of partitioning techniques in sorting algorithms
  • Basic concepts of recursion in programming
NEXT STEPS
  • Study the implementation of QuickSort in Python or Java
  • Learn about the impact of pivot selection on QuickSort performance
  • Explore alternative sorting algorithms like MergeSort and their time complexities
  • Investigate optimization techniques for QuickSort, such as using median-of-three pivot selection
USEFUL FOR

Computer science students, software developers, and algorithm enthusiasts interested in understanding sorting algorithms and their performance characteristics.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

The following pseudocodes are given.
Code:
 quicksort(A,p,r)
     if p<r then
        q<-partition(A,p,r)
        quicksort(A,p,q-1)
        quicksort(A,q+1,r)

Code:
   partition(A,p,r){
      x<-A[r]
      i<-p-1
      for j<-p to r-1
           if A[j]<=x then
              i<-i+1
              swap(A[i],A[j])
      swap(A[i+1],A[r])
      return i+1

Which value [m]q[/m] does partition return when all the elements of the array have the same value?

Which is the execution time of [m]quicksort[/m] if all the elements of the array [m]A[/m] have the same value?Show that if [m]A[/m] contains different elements and is sorted in decreasing order, the execution time of [m]quicksort[/m] is $\Theta(n^2)$.Show that the best case running time of [m]quicksort[/m] at an array with pairwise different elements is $O(n \lg n)$ .
I thought that the value that the function [m]partition[/m] returns when all the elements of the array have the same value is equal to $q=n$.In this case $q-1-p+1=q-p=n-p$ and $r-q-1+1=r-q=r-n$, right?So $T(n)=T(n-p)+T(r-n)+\Theta(n)$.Can we suppose that $p=1$ and $r=n$?
 
Technology news on Phys.org
I want to show that if $A$ contains distinct elements and is sorted in decreasing order, then the execution time of quicksort is $\Theta(n^2)$.

I thought the following:
The if-statement of partition will never be true, so it the function will return $q=1$.
So [m] quicksort(A,1,0) [/m] won't do anything and so the recurrence relation that describes the cost of quicksort will be:
$T(n)=T(n-1)+Θ(n)$.

Am I wrong? Because I found the following: http://www.math.lsa.umich.edu/~lspice/class/416/F2005/tex/2005Math416Homework6Solutions.pdf (page 2)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 34 ·
2
Replies
34
Views
5K
Replies
9
Views
3K