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...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
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).
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT