Question

In: Computer Science

What is the running time of QUICKSORT on an array A of length n containing all...

  1. 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.

Solutions

Expert Solution

Solution

QUICKSORT(A[], low, high)

{

if (low < high);

p = PARTITION(A, low, high);

//p is the partitioning index and A[p] is in the correct position.

QUICKSORT(A[], low, p - 1);

QUICKSORT(A[], p + 1, high);

}

PARTITION(A[], low, high)

{

pivot = A[high];

i = (low - 1) // i is the index of element smaller than pivot element

for (j = low; j <= high- 1; j++)

{

if (A[j] < pivot)

{

i++;

swap A[i] and A[j]

}

}

swap A[i + 1] and A[high]

return (i + 1)

}

All the elements have same value

When all elements in the array A[p..r] have the same value value returned by PARTITION is r.

Let array A has n elements. Index of the first element is 0 and index of the last element is n-1.

The first call to QUICKSORT is with low = 0 and high = n-1.

QUICKSORT(A[], 0, n-1) calls PARTITION(A[], 0, n-1).

Since all the elements have same value as pivot element A[n-1] , so i = n-2. In the return statement in PARTITION procedure the value returned is i+1 = n-1.

So the pivot elemnt A[n-1] is at correct position.

Next call to QUICKSORT is with low = 0 and high = n-2.

QUICKSORT(A[], 0, n-2) calls PARTITION(A[], 0, n-2).

Since all the elements have same value as pivot element A[n-2] , so i = n-3. In the return statement in PARTITION procedure the value returned is i+1 = n-2.

So the pivot elemnt A[n-2] is at correct position.

So the PARTITION procedure is called n times for n elements and each time the size of the array we are working with reduces with just one.

So the time complexity of the Quicksort algorithm in this case is: T(n)=O(n)+T(0)+T(n-1)=O(n)+T(n-1) = O(n2)

It is the most unbalanced case, in which a single quicksort call involves O(n) work and two recursive calls on lists of size 0 and n−1.

Different elements in decreasing order.

Let array A has n elements. Index of the first element is 0 and index of the last element is n-1.

The first call to QUICKSORT is with low = 0 and high = n-1.

QUICKSORT(A[], 0, n-1) calls PARTITION(A[], 0, n-1).

All the elements are scanned from index 1 to n-2. Since none of the elements have value less than pivot element A[n-1] so A[0] and A[n-1] are swapped.

Index value returned to calling QUICKSORT is 0, so p = 0.

Now the size of the array we have to scan is n-1.(from index 1 to index n - 1)

The second call to QUICKSORT is with low = 1 and high = n-1.

QUICKSORT(A[], 1, n-1) calls PARTITION(A[], 1, n-1).

All the elements are scanned from index 2 to n-2. Since none of the elements have value less than pivot element A[n-1] so pivot element A[n-1] remains at same index.

Index value returned to calling QUICKSORT is n-1, so p = n-1.

Now the size of the array we have to scan is n-2.(from index 1 to index n - 2)

So for each call to QUICKSORT, we are partitioning the array of size n to 2 subarrays.One subarray has size 0 and another subarray has size n-1

This is also an unbalanced case.

So the time complexity of the Quicksort algorithm in this case is: T(n)=O(n)+T(0)+T(n-1)=O(n)+T(n-1) = O(n2)


Related Solutions

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)
Consider an array of length n containing positive and negative integers in random order. Write the...
Consider an array of length n containing positive and negative integers in random order. Write the C++ code that rearranges the integers so that the negative integers appear before the positive integers. write a program that includes both functions and a main() function that tests them. Name the two functions rearrangeN() and rearrangeN2().
(do not need to code) Consider QuickSort on the array A[1:n] andassume that the pivot...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are =< x and all elements in the right portion A[m:hi] are >=x) is the first element of the array to be split (i. e., A[lo]).Construct an infinite sequence of numbers for n and construct an assignment of the numbers 1...n to the n array elements that causes QuickSort,...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements that...
P1 Consider an array of length n that contains unique integers between 1 and n+1. For...
P1 Consider an array of length n that contains unique integers between 1 and n+1. For example, an array A1 of length 5 might contain the integers 3, 6, 5, 1, and 4. In an array of this form, one number between 1 and n+1 will be missing. In A1, the integer 2 is missing from the array. As another example, consider the array A2 that contain the integers 4, 7, 5, 2, 6, and 1. A2 has size 6,...
Write a C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
Quicksort - Divider and Conquer Illustrate the operation of PARTITION on the array: A = {9,...
Quicksort - Divider and Conquer Illustrate the operation of PARTITION on the array: A = {9, 19, 13, 5, 12, 8, 7, 4, 21, 6, 11}. Let A[1] = 9 be the pivot value.
You are given an array of length n that is filled with two symbols (say 0...
You are given an array of length n that is filled with two symbols (say 0 and 1); all m copies of one symbol appear first, at the beginning of the array, followed by all n − m copies of the other symbol. You are to find the index of the first copy of the second symbol in time O(logm). include: code,  proof of correctness, and runtime analysis  
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT