GATE CSE
First time here? Checkout the FAQ!
x
+7 votes
245 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
asked in Algorithms by Active (1.7k points)   | 245 views
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?

1 Answer

+5 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
answered by Veteran (283k points)  

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.


Top Users Apr 2017
  1. akash.dinkar12

    3508 Points

  2. Divya Bharti

    2542 Points

  3. Deepthi_ts

    2040 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Shubham Sharma 2

    1610 Points

  7. Debashish Deka

    1588 Points

  8. Arunav Khare

    1454 Points

  9. Kapil

    1424 Points

  10. Arjun

    1420 Points

Monthly Topper: Rs. 500 gift card

22,076 questions
28,040 answers
63,230 comments
24,135 users