An implementation of quicksort has its best case of O(nlogn) on an array that is already sorted (smallest to largest). What case is an inverse sorted array (i.e. largest to smallest), and what will be the time complexity? Explain.
Worst-case analysis:
In inverse sorted array (largest to smallest) the quick-sort give the worst case because in this if the pivot selected is either the smallest or the largest number then the sublist returned by partitioning routine (partition function) is of size n-1.
If this happens recursively in every partition, then each
recursive call processes a list of size one less than the previous
list. Thus, we make n-1 nested calls before we reach a
list of size 1. This means that the call tree is a linear chain of
n-1 nested calls. The ith call does
O(n-i) work to do the partition, and thus the summation of
the whole partitioning operation is
.
Hence, in this case, the quicksort takes O(n2) time.
Even in an increasingly sorted array (smallest to largest), it gives the time complexity of O(n2), because here also the sublist returned by the partitioning routine (partition function) is of size n-1 if the pivot selected is smallest or largest number.
Best-case analysis:
The best-case occurs if each time we perform a partition we divide the list into two nearly equal pieces. So, each recursive call processes a list of half the size. Thus, we make only log2 n nested calls before we reach a list of size 1. This means that the depth of the call tree is log2 n and as a result, the algorithm uses only O(n log n) time.
An implementation of quicksort has its best case of O(nlogn) on an array that is already...
The worst-case complexity of the Quicksort is O(n2). (a) Use the selection algorithm to modify Quicksort and reduce its worst-case complexity to O( n*log(n) ) (b) Write the recurrence equation for the modified Quicksort algorithm.
What is big-theta runtime of Merge sort and Quicksort on an almost sorted array? i.e. the array is sorted except that two elements are swapped. Explain.
THESE ARE TRUE/FALSE The best-time complexity for insertion sort is O(nlogn). The worst-time complexity for bubble sort is O(nlogn). A linked structure consists of nodes. Each node is dynamically created to hold an element. All the nodes are linked together to form a list. The time complexity for searching an element in a binary search tree is O(logn) The time complexity for inserting an element into a binary search tree is O(logn). In an AVL tree, the element just inserted...
4. In an array-based implementation of the ADT list, what is the best case performance of the contains method? a. O(1) b. O(log n) C. O(n) d. O(nº) 5. In an array-based implementation of the ADT list, what is the worst case performance of the contains method? a. O(n) b. O(n) C. O(log n) d. 0(1) 6. In an array-based implementation of the ADT list, what is the performance of the contains method when the entry is not in the...
1.3 Which of the following is true about sorting functions?A.The most optimal partitioning policy for quicksort on an array we know nothing about wouldbe selecting a random element in the array.B.The fastest possible comparison sort has a worst case no better than O(n log n).C.Heapsort is usually best when you need a stable sort.D.Sorting an already sorted array of size n with quicksort takes O(n log n) time.E.When sorting elements that are expensive to copy, it is generally best to...
Given an unsorted array. The array has this property that every element in array is at most k distance from its position in sorted array where k is a positive integer smaller than size of array. Which sorting algorithm can be easily modified for sorting this array and what is the obtainable time complexity? Group of answer choices nsertion Sort with time complexity O(kn) Heap Sort with time complexity O(nLogk) Quick Sort with time complexity O(kLogk) Merge Sort with time...
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 :=...
5. Answer the following.
(a) (5 points) Suppose you are given a maxheap of n unique
numbers. Explain where will the smallest of these n numbers be
located in the maxheap. Explain where will the second largest
number be located on this maxheap. Please be specific.
(b) (5 points) Suppose you are given an array A of n numbers,
where all the elements of the array are already sorted in
decreasing order. Is this a max-heap? Explain.
(c) (5 points)...
Consider the array a = {3, 5, 2, 6, 0, 2, 1} Trace the execution of QuickSort(a). What is the best, average, and worst-case time complexity of QuickSort?
2. Consider QuickSort algoriothm, with the middle element of the input array always used as the pivot, (a) What is the asymptotic running time (expressed as Big-O) for sorted input (2 points) (b) What is the asymptotic running time (expressed as Big-O) for reverse-ordered input (2 points) (c) Which of the above arrays will be sorted faster? (2 points)