Computer Sciences > Gate 2014 Set-1 > Sorting Algorithm
Let P be a QuickSort Program to sort numbers in ascending order using the first element as pivot. Let t1 and t2 be the number of comparisons made by P for the inputs [1, 2, 3, 4, 5] and [4, 1, 5, 3, 2] respectively. Which one of the following holds?
A
t1 = 5
B
t1 < t2
C
t1 > t2
D
t1 = t2

Correct : Sorting Algorithm

The question asks us to compare the number of comparisons made by QuickSort when the first element is always chosen as the pivot.
Analyze the Options:
Input 1: [1, 2, 3, 4, 5]
The first element, 1, is the smallest element, so every other element is compared with the pivot. This creates the worst-case pattern, where the list is divided as 0 and 4, then 0 and 3, and so on.
Comparisons = 4 + 3 + 2 + 1 = 10.
Input 2: [4, 1, 5, 3, 2]
The first pivot is 4. The elements 1, 3, and 2 go to the left, while 5 goes to the right. The resulting subarrays are smaller and require fewer comparisons than the first input.
Comparisons = 4 + 3 + 1 = 8.
Comparison:
t1 = 10 and t2 = 8.
Therefore, t1 > t2.
Correct Answer: c) t1 > t2

Similar Questions

Assume that a mergesort algorithm in the worst case takes 30 seconds for an input of size 64. Which of the following most closely approximates the maximum input...
#26 MCQ
A palindrome is a word that reads the same forwards and backwards. In a game of words, a player has the following two plates painted with letters. From...
#1 MCQ
Which number does not belong in the series below? 2, 5, 10, 17, 26, 37, 50, 64
#4 MCQ

Related Topics

QuickSort QuickSort comparisons first element pivot algorithm analysis sorting algorithms GATE CS 2014 Q3 Computer Sciences GATE

Unique Visitor Count

Total Unique Visitors

Loading......