Execution Time of QuickSort for Different Inputs

In summary, the conversation discusses the pseudocode for quicksort and its execution time in different scenarios. The pseudocode includes a function called partition which returns a value q. When all elements of the array have the same value, partition returns q=n. In this case, the execution time of quicksort is $\Theta(n^2)$. However, if A contains different elements and is sorted in decreasing order, the execution time of quicksort is $\Theta(n^2)$. It is also noted that the best case running time of quicksort for an array with pairwise different elements is $O(n \lg n)$. The conversation also explores the possibility of assuming certain values for p and r in the recurrence relation for quicksort's execution time
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
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)
 

What factors affect the execution time of a function?

The execution time of a function can be affected by a variety of factors, such as the complexity of the function, the amount of data being processed, the hardware and software environment in which the function is running, and the efficiency of the coding and algorithms used in the function.

How can I measure the execution time of a function?

The execution time of a function can be measured by using a profiler tool, which tracks the time it takes for each line of code to execute. Alternatively, you can add timing code within the function itself to calculate the start and end times and then calculate the difference.

Why is it important to optimize the execution time of a function?

Optimizing the execution time of a function is important for improving the overall performance and efficiency of a program. Faster execution times can lead to quicker processing of data, better user experience, and cost savings for companies hosting their applications on cloud services.

What are some techniques for reducing the execution time of a function?

One technique for reducing execution time is to use efficient algorithms and data structures in the function. Another approach is to parallelize the function, splitting it into smaller tasks that can be executed simultaneously on multiple processors. Additionally, optimizing the code by removing unnecessary operations and minimizing memory usage can also improve execution time.

Can the execution time of a function be predicted?

The execution time of a function cannot be predicted with absolute certainty, as it can vary depending on the factors mentioned above. However, by analyzing the function's code and understanding its dependencies and external influences, we can estimate its execution time and make improvements accordingly.

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
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top