retagged by
1,658 views
1 votes
1 votes
Consider the following array...

12 18 17 11 13 15 16 14

Find the no of elements which will change  their position when partitioning algorithm is applied on array .pivot choosen is 15
retagged by

5 Answers

1 votes
1 votes

We can choose any random element as pivot

Here 3rd last element 15 as pivot 

 So, as 17, 18 greater than pivot ,17, 18 will go after 15

and 14 less than pivot, so,14 will come before 15

12,11,13,14,15,18,17,16......................here 3 element change its position

Now 13 pivot, No change in element

Now 12 as pivot 11 will change its position...........here 1 element change position

11,12,13,14,15,17,16,18

Now 18 as pivot 2 element change its position

12,11,13,14,15,17,16,18........................here 2 element change position

Now 17 as pivot 16 will change its position..............here 1 element change its position

Here  total no of element change its position is 7

edited by
1 votes
1 votes

12, 18, 17, 11, 13, 15, 16, 14.   15 is pivot swap it with right most element

12, 18, 17, 11, 13, 14, 16, | 15

12, 11, 17, 18, 13, 14, 16, | 15

12, 11, 13, 18, 17, 14, 16, | 15

12, 11, 13, 14, 17, 18, 16, | 15

12, 11, 13, 14, 15, 18, 16, | 17

12, 11, 13, 14, 15, 18, 16, 17

clearly 12 and 16 are not changed.

if we use pivot as leftmost element then 16 doesn't change

12, 18, 17, 11, 13, 15, 16, 14.   15 is pivot swap it with left most element

15 | 18, 17, 11, 13, 12, 16, 14

15 | 11, 17, 18, 13, 12, 16, 14

15 | 11, 13, 18, 17, 12, 16, 14

15 | 11, 13, 12, 17, 18, 16, 14

15 | 11, 13, 12, 14, 18, 16, 17

14 | 11, 13, 12, 15, 18, 16, 17

in this case answer is seven

edited by
1 votes
1 votes

METHOD 1 : Pivot Choosing as Middle Element

step1: Elements greater than 15 move  to right hand side of 15  in same order.

step2: Elements which are less than 15 put in same order when they are LHS of 15 else move them to ahead of 15 in same order.

step3: Fix the position of 15.

After doing this arrangements of array elements are:  

12 11 13 14 15 18 17 16 

Ans=7

METHOD 2 : Pivot Position is choosen as Right Most Element

12, 18, 17, 11, 13, 15, 16, 14.   15 is pivot swap it with right most element
12, 18, 17, 11, 13, 14, 16, | 15
12, 11, 17, 18, 13, 14, 16, | 15
12, 11, 13, 18, 17, 14, 16, | 15
12, 11, 13, 14, 17, 18, 16, | 15
12, 11, 13, 14, 15, 18, 16, | 17
12, 11, 13, 14, 15, 18, 16, 17
clearly 12 and 16 are not changed.

Ans= 6
 

METHOD 3 : Pivot Position is choosen as Left Most Element

if we use pivot as leftmost element then 16 doesn't change

12, 18, 17, 11, 13, 15, 16, 14.   15 is pivot swap it with left most element
15 | 18, 17, 11, 13, 12, 16, 14
15 | 11, 17, 18, 13, 12, 16, 14
15 | 11, 13, 18, 17, 12, 16, 14
15 | 11, 13, 12, 17, 18, 16, 14
15 | 11, 13, 12, 14, 18, 16, 17
14 | 11, 13, 12, 15, 18, 16, 17

Ans = 7

Clearly this is Ambiguous Question . We need to know the position of Pivot Element   .  Made Easy has asked the same in Last Year Test Series as well

0 votes
0 votes
12 18 17 11 13 15 16 14
First swap the pivot element to first place of array so 15 18 17 11 13 12 16 14

Now after the partition algo     ( i followed this algo)
partition(a,p(first element),q(last element))

{
x=a[p];
i=p;
for(j=p+1;j<=q;j++)

{
         if(x>=a[j])
                 {                      
                       i=i+1;
                     swap(a[i],a[j]);
                  }
             swap(a[i] ,a[p]);
            return 0 ;
}

e.g  
                                  x
                                  i  j
                                 15 18 17 11 13 12 16 14  condition fails
                                         j
                                  15 18 17 11 13 12 16 14  condition fails
                                      i     j
                                  15 18 17 11 13 12 16 14  condition true i will be incremented swap a[i],a[j]
                                  15 11 17 18 13 12 16 14 after swap again loop will check
                                         i    j  
                                  15 11 17 18 13 12 16 14 condition true i will be incremented swap a[i],a[j]
                                  15 11 13 18 17 12 16 14 after swap again loop will check
                                            i     j
                                  15 11 13 12 17 18 16 14 condition true i will be incremented swap a[i],a[j]
                                  15 11 13 12 17 18 16 14 after swap again loop will check
                                            i        j
                                  15 11 13 12 17 18 16 14  condition fails
                                               i        j
                                  15 11 13 12 17 18 16 14 condition true i will be incremented swap a[i],a[j]
                                  15 11 13 12 14 18 16 17 here j>q so loop ends
                 
       now swap a[i] a[p]         14 11 13 12 15 18 16 17  

    14 11 13 12 15 18 16 17
So 7 element changed their position after the partition algo.

Related questions

1 votes
1 votes
1 answer
1
Saurabh Sharma asked Jul 22, 2015
1,074 views
THISCOURSEISOVERChoose the last elements as pivot elements (R). Also for duplicates, adopt the convention that both pointers stop.a) EHIOCOIERRUSSVTSb) EHISCOIERRUSOVTSb)...
2 votes
2 votes
1 answer
3
gate_forum asked Dec 18, 2015
745 views
probability of a split more balanced than a to (1-a) split in partitioning procedure of quicksort where 0<a<=1/2?1-3a 1-2a a+1 none