293 views
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
I am not able to understand question completaly. i have tried to solve it by using 1st element of given option as pivot and then tried to do partitioning around it. I think my approch is wrong. Someone guide me on this?

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
answered by Veteran (319k points) 577 1445 2962
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?
You are welcome. Need to see again- if one of the two can be eliminated due to some reason.

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.

??

yes, but "5" could have been the pivot in previous step rt?
Both are answer i tried for pettern also . but will fail but elements same .

1st one order = 69897

2nd one = 45533
@arjun sir keys given in any option r need not in orderd way i think these r permutation of that ??
vijay take 5 as pivot . then you have to left one 5. only two 5 present
@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 ??
vijay i give you idea take 1st 5 as pivot then ake 2 as pivot you get 45533 as one partition.

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 ?

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, 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

Got it now, thank you @Anirudh , @Arjun sir .  : )
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.