Which of the following sorting algorithms can be inferred from the worst-case complexity of the following rel
O(nlogn)?
Relation
T(n) = T(n-1) + Cn, n > 1
T(1) = C1, n=1
1 Merge sort
2 Heap sort
3 insertion sort
4 Quick sort
Answers
Explanation:
Assume that the algorithms considered here sort the input sequences in ascending order. If the input is already in ascending order, which of the following are TRUE ?
I. Quicksort runs in Θ(n2) time
II. Bubblesort runs in Θ(n2) time
III. Mergesort runs in Θ(n) time
IV. Insertion sort runs in Θ(n) time
(A) I and II only
(B) I and III only
(C) II and IV only
(D) I and IV only
Answer: (D)
Explanation: I. Given an array in ascending order, Recurrence relation for total number of comparisons for quicksort will be
T(n) = T(n-1)+O(n) //partition algo will take O(n) comparisons in any case.
= O(n^2)
II. Bubble Sort runs in Θ(n^2) time
If an array is in ascending order, we could make a small modification in Bubble Sort Inner for loop which is responsible for bubbling the kth largest element to the end in kth iteration. Whenever there is no swap after the completion of inner for loop of bubble sort in any iteration, we can declare that array is sorted in case of Bubble Sort taking O(n) time in Best Case.
III. Merge Sort runs in Θ(n) time
Merge Sort relies on Divide and Conquer paradigm to sort an array and there is no such worst or best case input for merge sort. For any sequence, Time complexity will be given by following recurrence relation,
Answer:
i & iv
Explanation:
This question is not complete
Assume that the algorithms considered here sort the input sequences in ascending order.
I. Quicksort runs in Θ(n2) time
II. Bubblesort runs in Θ(n2) time
III. Mergesort runs in Θ(n) time
IV. Insertion sort runs in Θ(n) tim
Answer: i & iv
#SPJ2