Consider a modification to QuickSort where each time the partition function is called, the median of the partition array is always found (in constant time) and used as the pivot. The worst-case running time for the algorithm is: Group of answer choices Θ(nlogn) Θ(n^2) Θ(n) Θ(logn)
Question
Consider a modification to QuickSort where each time the partition function is called, the median of the partition array is always found (in constant time) and used as the pivot.
The worst-case running time for the algorithm is:
Group of answer choices Θ(nlogn) Θ(n^2) Θ(n) Θ(logn)
Solution 1
The correct answer is Θ(nlogn).
When the median is used as the pivot in QuickSort, the partitioning is always balanced and the height of the recursion tree is always log(n). Therefore, the time complexity is Θ(nlogn) in all cases (best, average, and worst), because at each level of the recursion tree we do Θ(n) work and there are log(n) levels.
Solution 2
The correct answer is Θ(nlogn).
When the median is used as the pivot in QuickSort, the partitioning is always balanced and the height of the recursion tree is always log(n). Therefore, the time complexity is Θ(nlogn) in all cases (best, average, and worst), because at each level of the recursion tree we do Θ(n) work and there are log(n) levels.
Similar Questions
The O(n log n) average-case running time analysis of quicksort relies on several assumptions. Which of the following does not need to be assumed:Group of answer choicesThat each function call can be carried out in O(1) timeThat each comparison of two elements can be carried out in O(1) timeThat the result of each partitioning step is an arrangement in which the pivot value is equally likely to end up in each position of the partitioned arrayThat the result of each partitioning step is an arrangement in which the pivot value ends up close to the middle of the partitioned arrayThat each swap of two elements can be carried out in O(1) time
Quicksort uses Hoare partitioning. Assume an array contains ten keys: 6 3 1 7 9 5 8 2 4 0. After a first round of simple Hoare partitioning (not median-of-three), the array looks like so: Group of answer choices 5 3 1 0 4 2 6 8 9 7 2 3 1 0 4 5 6 8 9 7 3 1 5 2 4 0 6 7 9 8 2 3 0 1 5 4 6 7 8 9 5 3 1 4 0 2 6 7 9 8
Which sorting algorithm selects a pivot element and partitions the array around it?Options: Pick one correct answer from belowBubble SortMerge SortQuick SortRadix Sort
def partition(array, low, high): pivot = array[high] i = low - 1 for j in range(low, high): if array[j] <= pivot: i += 1 array[i], array[j] = array[j], array[i] array[i+1], array[high] = array[high], array[i+1] return i+1def quicksort(array, low=0, high=None): if high is None: high = len(array) - 1 if low < high: pivot_index = partition(array, low, high) quicksort(array, low, pivot_index-1) quicksort(array, pivot_index+1, high)my_array = [64, 34, 25, 12, 22, 11, 90, 5]quicksort(my_array)print("Sorted array:", my_array)
The running time of quick sort depends heavily on the selection of Select one:Pivot elementNo of inputs Arrangement of elements in array Size of elements
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.