MHB Execution Time of QuickSort for Different Inputs

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Function Time
AI Thread Summary
The discussion focuses on analyzing the quicksort algorithm, particularly its behavior under specific conditions. When all elements of the array are identical, the partition function returns the index equal to the length of the array, leading to a recurrence relation that suggests a linear time complexity. In contrast, when the array contains distinct elements sorted in decreasing order, the partition function's if-statement fails, resulting in a recurrence relation that indicates a quadratic time complexity of Θ(n²). Additionally, the best-case scenario for quicksort, with distinct elements, achieves a time complexity of O(n log n). The conversation includes attempts to clarify these complexities and validate the reasoning behind the recurrence relations.
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)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top