in Algorithms
1,480 views
10 votes
10 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
in Algorithms
1.5k views

1 comment

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?
0
0

1 Answer

6 votes
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
by

4 Comments

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

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