Let Xij =I { zi is compared to zj in quick sort algorithm} is an indicator variable, and zi is the ith smallest number. Which of the following sequence of the numbers give the X2,5 = 1? Note: Assume that there is a divide and conquer quick sort algorithm and in partition algorithm the first element of the lists is taken as pivot-element.

Let Xij =I { zi is compared to zj in quick sort algorithm} is an indicator variable, and zi is the ith smallest number. Which of the following sequence of the numbers give the X2,5 = 1? Note: Assume that there is a divide and conquer quick sort algorithm and in partition algorithm the first element of the lists is taken as pivot-element. Correct Answer 10, 20, 50, 30, 40

The correct answer is option 2.

Concept:

QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

  • Always pick the first element as a pivot.
  • Always pick the last element as the pivot.
  • Pick a random element as a pivot.
  • Pick median as the pivot.

Here given, Assume that there is a divide and conquer quick sort algorithm and in the partition algorithm, the first element of the lists is taken as pivot-element.

Option 1: 40, 50, 10, 20, 30

False, Here the pivot element is 40. After running quick sort the sequence will be like 20, 50, 10, 40, 30. So the given sequence of the numbers X2,5 = 1 is not correct.

Option 2: 10, 20, 50, 30, 40

True, Here the pivot element is 10. After running quick sort the sequence will be like 10, 20, 50, 30, 40. So the given sequence of the numbers X2,5 = 1 is the correct sequence.

After the pivot is 20, it divides the sequence like 10, 20, 50, 30, 40. And X3,5 = 1

After the pivot is 50, it divides the sequence like 10, 20, 40, 30, 50.  And X4,5 = 1. So on.

Option 3: 40, 20, 10, 30, 50

False, Here the pivot element is 40. After running quick sort the sequence will be like 30, 20, 10, 40, 50. So the given sequence of the numbers X2,5 = 1 is not correct.

Option 4: 30, 20, 10, 40, 50

False, Here the pivot element is 30. After running quick sort the sequence will be like 10, 20, 30, 40, 50. So the given sequence of the numbers X2,5 = 1 is not correct.

Hence the correct answer is 10, 20, 50, 30, 40.

Additional InformationQuicksort:

The worst-case time complexity quicksort algorithm is O(n2) if the array is sorted.
T(n) = T(n-1) + cn

That's why randomized quick sort come to make time complexity O(N log N)
Randomized quicksort:
Randomized quicksort says instead of picking the pivot to be the first element what if pick my pivot to be a random element from my array A.

Related Questions