The Gateway to Computer Science Excellence

+9 votes

There are many variations of Quicksort. We may choose the pivot for each partition step in various ways. There are various strategies for partitioning an array segment into one subpartition of consecutive array positions that has values less than or equal to the pivot and another subpartition of consecutive array positions with those values greater than the pivot. The recursion that partitions the array into smaller and smaller segments may be stopped in various ways. However, even with all that variation, not any set of values can ever be a partition at any level. Your problem is to consider the Quicksorting of an array that initially has the values 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7 and identify below the one set of values that could possibly be one entire partition at some level of the Quicksort recursion

a) 6, 9, 7, 8, 9

b) 3, 1, 3, 1

c) 5, 4, 3, 3, 5

d) 1, 4, 1, 3

a) 6, 9, 7, 8, 9

b) 3, 1, 3, 1

c) 5, 4, 3, 3, 5

d) 1, 4, 1, 3

+6 votes

Question is very unique from normal ones. But definite possibility of being asked for GATE.

3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7

The thing is any partition of a quick sort will need to be consecutive sequence in the final sorted array. So, lets sort the given array of 14 elements. We get

1 1 2 3 3 4 5 5 5 6 7 8 9 9

Now, we can see if we get a contiguous space for each of the choices.

a) 6, 9, 7, 8, 9 - possible in the last 5 places.

b) 3, 1, 3, 1 - not possible as 2 must be inside them

c) 5, 4, 3, 3, 5 - possible from places 3-7 (starting from 0)

d) 1, 4, 1, 3 - not possible as one 2 and 1 3 is missing in between

3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7

The thing is any partition of a quick sort will need to be consecutive sequence in the final sorted array. So, lets sort the given array of 14 elements. We get

1 1 2 3 3 4 5 5 5 6 7 8 9 9

Now, we can see if we get a contiguous space for each of the choices.

a) 6, 9, 7, 8, 9 - possible in the last 5 places.

b) 3, 1, 3, 1 - not possible as 2 must be inside them

c) 5, 4, 3, 3, 5 - possible from places 3-7 (starting from 0)

d) 1, 4, 1, 3 - not possible as one 2 and 1 3 is missing in between

0

Thank you so much Arjun sir, Now i am able to understand what sort of answer was expecting in this type of question! But sir, we got two possibility here, is their one option which would be more appropriate?

0

very nice question (y).

Arjun Sir, since we are asked to identify the one set of values that could possibly be one entire partition at some level of the Quicksort recursion.

So, If we see option C. 5, 4, 3, 3, 5. then here one 5 is missing in this set, because if it is one entire partition then it must contain all three 5.

??

0

Both are answer i tried for pettern also . but will fail but elements same .

1st one order = 69897

2nd one = 45533

1st one order = 69897

2nd one = 45533

0

0

@Anirudh, if we take 5 as pivot then how come 3, 4 in the partition. ??

Can you, please write partition after taking 5 as pivot ??

Can you, please write partition after taking 5 as pivot ??

0

okay , I am trying to do as you are saying .. plzz check--

3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7

taking first 5 as a pivot-

3, 1, 4, 1, 3, 2, 5, 6, 5, 9, 5, 8, 9, 7

now taking 2 as a pivot in the first partition-

1, 1, 2, 4, 3, 3, 5, 6, 5, 9, 5, 8, 9, 7 //

now say what to do ??

Or I am doing something wrong ?

+3

take last 5 as pivot

**5,** 1, 4, 1, 3, 2, 5, 6, 5, 9, **3,** 8, 9, 7

after fix pivot looks

**(**3**,** 1, 4, 1, 3, 2, 5, 5) 5, , (6, 9, 8, 9, 7)

take 1st partition and 2 as pivot

**(2****,** 1, 4, 1, 3,**3** , 5, 5)

after correct place of 2

(1,1 ), 2 ,( 4,3,3,5,5)

+3

3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7

6 as pivot

3 1 4 1 5 2 5 3 5 6 9 8 9 7

Now, 2 as pivot

1 1 2 3 3 4 5 5 5

Now, 5 as pivot

3 3 4 5 5

0

Sir, my result after partitioning is not same as yours, i am getting (3,1,4,1,5,5,2,3,5) ,6, (9,8,9,7). what i am doing is after taking 6 as pivot, i am replacing it with first element of array and then applying quicksort. as quicksort can be applied in various fashion, what my strategy is start from the 2nd element as first one is pivot and find first occurrence which is > than pivot at the same time, start from the end of array and find first occurrence of element <= pivot. and then swap them and repeat till two ptr cross each other.

Sir i want to know, if i consider set form , i am getting the answer but as far as partition is considered, i am not getting same partition as yours.

May be it's lame question but i wanted to be sure. pls provide your feedback sir.

Sir i want to know, if i consider set form , i am getting the answer but as far as partition is considered, i am not getting same partition as yours.

May be it's lame question but i wanted to be sure. pls provide your feedback sir.

52,345 questions

60,489 answers

201,829 comments

95,296 users