what’s T(n) of the QuickSort algorithm in (1) the best case, (2)
the worst
case and (3) the case where the partition() algorithm always splits
the input
array with a 40:60 ratio (i.e., 40% of data goes in one partition
and the
remaining 60% the other)?
algorithm quicksort(A, lo, hi)
if lo < hi then
p := partition(A, lo, hi)
quicksort(A, lo, p - 1 )
quicksort(A, p + 1, hi)
algorithm partition(A, lo, hi)
pivot := A[hi]
i := lo - 1
for j := lo to hi - 1 do
if A[j] < pivot then
i := i + 1
swap A[i] with A[j]
if A[hi] < A[i + 1] then
swap A[i + 1] with A[hi]
return i + 1
Answer :
BEST CASE : T(n) = 2T(n/2) + n
Explanation : Best CASE Occurs when the PIVOT is
taken in the Middle of the array and we divide the array into two
halves equally ..So we have two halves of n/2 and n/2 hence we
write T(n/2) + T(n/2) which becomes 2T(n/2).
The term n comes as addition because at every step we partition the
array and Paritioning takes n time
Hence combining both of them we get 2T(n/2) + n
Worst Case : T(n) = T(n - 1) + n
Explanation : WORST CASE Occurs when the INPUT IS SORTED
and my Array will divide in 1 and n-1 elements
The term n comes as addition because at every step we partition the
array and Paritioning takes n time
Hence combining both of them we get T(n-1) + n
3) Case where partition is 40: 60 that means 2n/5 and
3n/5
=> T(n) = T(2n/5) + T(3n/5) + n
Explanation: Array will be divided in 40:60 hence we get
T(2n/5) for 40 division : i.e 40*n/100 and T(3n/5)
for 60 division : i.e 60*n/100.The term n comes as addition because
at every step we partition the array and Paritioning takes n time.
Hence combining both of them we get T(2n/5) + T(3n/5) + n
what’s T(n) of the QuickSort algorithm in (1) the best case, (2) the worst case and...
Worst-case complexity of quicksort is similar to the every-case complexity of exchange sort. Average-case time complexity of quicksort is as good as worst-case time complexity of mergesort. Similar to quicksort, mergesort uses a pivot to partition the input array. Select one: True False
1. (10 pts total) For parts (1a) and (1b), justify your answers in terms of deterministic QuickSort, and for part (1c), refer to Randomized QuickSort. In both cases, refer to the versions of the algorithms given in the lecture notes for Week 3. (a) (3 points) What is the asymptotic running time of QuickSort when every element of the input A is identical, i.e., for 1 ≤ i,j ≤ n, A[i] = A[j]? Prove your answer is correct. (b) (3...
In an effort to balance the distribution (length) of partitions created in the Quicksort algorithm so that worst case performance can be avoided, one can employ randomization, rather than selecting the element at a certain position as the pivot. Use your favorite programming language to implement the randomized Quicksort algorithm. So, you will need to use the following algorithms to implement it: RANDOMIZED-PArtitioN (A, p, r) 1 i = RANDOM (p, r) 2 exchange A[r] with A[i] 3 return PARTITION...
suppose that the partition () algorithm used by quicksort() always produces a 99-to-1 pproportional split, i.e, each time partition() is called, 99% of the date elements end up on one side of the pivot. use a recursion tree to derive a theta notation expression that describes the running time of quicksort() for this situation,
Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...
5. Calculate the worst-case scenario runtime for int P(int al, int low, int high) int t; int lo = (low <= high)?(low): (high); int hi = (high + low) - lo; int i = lo - 1; int pivot = a[hi]: for(int j = 10; j < hi;j +1) if (a[j] < pivot) i += 1; t = a[i]; a[i] = a[j]; a[j] = t; t = a[i+1]; a[i+1] = a[hi]: a[hi] = t; return (i + 1); where high...
6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...
And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...
Data Structure Question. I need help solving this question. I know quicksort has the worst case of O(n^2) if it is implemented choosing the pivot as the first element. A[1] is the first element here. Please justify why the number of comparison is the smallest possible number assuming the array ensures that. And give an example of that type of an array. Thank you thumbs up will be given for correct and justified answer! qs(A): if A has at most...
Find the best case, worst case and average case complexity for numbers of comparison and assignment operations for the following code. Indicate when there is no best or worst case. Comparisons Assignments Given 2-D array of integer map[n][n]: Best: Best: worst: worst: for (i0; 1 <n; i++) for(j = 0j <n; j++) If (map 10] < 0) map[001-mapli01: average: average: For ease of analysis, assume half of the elements in map are negative.