• retagged by
4,114 views
1 1 vote
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

5 Answers

1 1 vote

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 1 vote

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 1 vote

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 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.
0 0 votes

I think 7 is correct.Check?

#include<stdio.h>
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}
int main()
{
    int arr[]={15,18,17,11,13,12,16,14};
    int   p=0;
	int x = arr[p];
	int i = p ,j;
    for ( j =p+1; j<=7; j++)
	{
		if (arr[j] <= x)
		{
			i++;
			swap (&arr[i], &arr[j]);
		}
	}
      swap (&arr[p], &arr[i]);

	int k;
    for (k=0; k<=7; k++)
        printf("%d ", arr[k]);
        printf("\n");
}

Note:Above code picking first element as pivot.

Position:
Show:

Related questions

1 1 vote
1 1 answer
1.7k
1.7k views
Saurabh Sharma asked Jul 22, 2015
1,651 views
THISCOURSEISOVERChoose the last elements as pivot elements (R). Also for duplicates, adopt the convention that both pointers stop.a) EHIOCOIERRUSSVTSb) EHISCOIERRUSOVTSb)...
1 1 vote
1 1 answer
147
147 views
GO Classes asked Aug 10
147 views
While sorting the numbers $\text{(70, 48, 76, 58, 43, 47, 78, 53)}$ using quicksort, the last number is chosen as pivot, what will be the permutation of the numbers after...