Question

In: Computer Science

What is the running time of Quicksort when the input array is sorted in ascending order?...


What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)

Solutions

Expert Solution

Analysis of Time complexity of quick sort algorithm:

T(n) = T(k) + T(n-k-1) + (n)

Where k is the number of items less than pivot
n is number of items in array

The first two terms are included for the recursive quicksort calls.

The last term is for the partition algorithm used in quick sort.

Solution:

When the elements are already in sorted order, the smallest element is present at the start of array and the largest element is present at the end of array.

The worst case in the quick sort occurs when the algorithm selects the smallest or largest item as pivot.

The last element here is the largest element.

So, the worst case occurs when the array is already sorted.

The worst case time is computed as shown below.

T(n) = T(0) + T(n-0-1) + (n)

= T(n-1) + (n)

= ()

Therefore,  the running time of Quicksort when the input array is sorted in ascending order is  ().


Related Solutions

What is the running time of QUICKSORT on an array A of length n containing all...
What is the running time of QUICKSORT on an array A of length n containing all the same values? What is the running time if A contains distinct elements sorted in decreasing order? Explain your answers in detail, referring to the source code for PARTITION and QUICKSORT.
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.
Programming Language : JavaModify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array.After it has been sorted in descending order, go through all the items in the array and make sure...
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
What is the average performance of Quicksort assuming every input permutation is equally likely? Prove it....
What is the average performance of Quicksort assuming every input permutation is equally likely? Prove it. Hint: In this case pivot(split) element very likely to provide good balance during partition.
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size 5. Show that, when the group size is 7, the algorithm still runs in linear time.
A certain refrigerator has a COP of 5.00. When the refrigerator is running, its power input...
A certain refrigerator has a COP of 5.00. When the refrigerator is running, its power input is 500 W. A sample of water of mass 500 g and temperature 20.0 ºC is placed in the freezer compartment. How long does it take to freeze the water to ice at 0ºC?ANS = 83.3 s I can't seem to get 83.3s
Derived the time response of first order system with the ramp input using Laplace transform.
Derived the time response of first order system with the ramp input using Laplace transform.
Can we predict the running time for Mr. Degges when he runs 3.1 miles on the...
Can we predict the running time for Mr. Degges when he runs 3.1 miles on the track at the NDSU Wellness center? Need: SAS output to analyze the model Need: prediction equation y-hat SSE SST, error, F-test What variables are significant The variables are: Y = running time in minutes X1 = weight at the time of running X2 = number of days between running events Year X1 X2   Y 2009 191.2 1 29.0 2009 192 1   27.80 2009 190.4...
Provide sql query to calculate average time between when an order is made and when it...
Provide sql query to calculate average time between when an order is made and when it is shipped. Use Avg() function to calculate average time.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT