Question

In: Computer Science

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 causes QuickSort, with the stated choice of pivot, to

(a) execute optimally (that is A[lo:m] and A[m:hi] are always of equal size)

(b) execute in the slowest possible way.

Solutions

Expert Solution

Answer : Given data

* It is given that the pivot element (x) is always chosen to be the first element of the array to be sorted using quicksort.

* If you want the algorithm for the method given in the question just use the regular quicksort where the pivot element is chosen the very first element passed to it (as mentioned by you initially). Else if you want the solution to the question follow :

* a - > In this part we are asked to give a sequence of numbers that makes the algorithm work optimally. So only input to the quicksort is asked and no algorithm in both parts.

* please note that the definition of optimality is total elements smaller than pivot element (x) should be equal to the total elements greater than pivot element (x).

* Let us take n = 15 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

the most optimal assignments of numbers is :

[8,4,2,1,3,6,5,7,12,10,9,11,14,13,15[

* Initiall we take pivot element (x) = first element of array i.e; 8 So, elements in the left side are [4,2,1,3,6,5,7)]and elements in the right side are[12,10,9,11,14,13,15)]i.e; both are 7 in number.

* Similarly when in the left subarray [4,2,1,3,6,5,7] pivot is 4 and division is as follows left = [2,1,3] right [6,5,7]

and right sub array [12,10,9,11,14,13,15] pivot is 12, left sub array[10,9,11] right sub array = [14,13,15]

* Similarly for all smaller sub arrays :

[2,1,3] pivot = 2 left sub array = [1] right sub array = [3]

[6,5,7] pivot = 6 left sub array = [5] right sub array = [7]

[10,9,11] pivot = 10 left sub array = [9] right sub array = [11]

[14,13,15] pivot = 14 left sub array = [13] right sub array = [15]

* So, we can see always the number of elements in the left and right sub array is always equal.

b - > the input for which the quicksort performs worst

* The worst case for quicksort (in this case ) and for any other case :

a. When array is already sorted i.e; 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

b. When array is sorted in reverse order i.e; 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1.

c. When all elements are same.

* So let us take first case:

* array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], pivot = 1, left sub array = [ ] (empty), right sub tree = [2,3,4,5,6,7,8,9,10,11,12,13,14,15]

* array = [2,3,4,5,6,7,8,9,10,11,12,13,14,15], pivot = 2, left sub tree = [ ] (empty), right sub tree = [3,4,5,6,7,8,9,10,11,12,13,14,15] and the process goes on dividing the array into two arrays left = 0 elements and right = n-1 arrays. And each process will take O(n) * so total time complexity = (n-1)O(n) i.e; O(n2) assymptotically.

________________THE END________________-


Related Solutions

. 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...
(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,...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
What is the reason to choose a random pivot element in Randomized Quicksort? How can using...
What is the reason to choose a random pivot element in Randomized Quicksort? How can using randomization in this way possibly help?
(Data structure) Show the contents of the array below, once the “pivot” element is placed at...
(Data structure) Show the contents of the array below, once the “pivot” element is placed at its appropriate location after each call of the “Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in descending order (from largest to smallest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set s,       f,       p,      a,      g,      e,       v,      q,      i,        c
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
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.
Consider the element uniqueness problem: check whether all the elements in a given array of n...
Consider the element uniqueness problem: check whether all the elements in a given array of n elements are distinct answer in pseudo code places
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of...
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of n integers for i ← 0 to n − 1 do s ← X[0] for j ← 1 to i do s ← s + X[j] A[i] ← s / (i + 1) return A ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Algorithm2 prefixAverages2(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of...
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT