The Gateway to Computer Science Excellence

+8 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

+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

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 ?

+2

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)

+2

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.

- All categories
- General Aptitude 1.9k
- Engineering Mathematics 7.5k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.3k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.1k
- Non GATE 1.5k
- Others 1.5k
- Admissions 595
- Exam Queries 576
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 17

50,647 questions

56,492 answers

195,439 comments

100,694 users