1,005 views
1 1 vote
In Quicksort of the following numbers, if the pivot is chosen as the first element, what will be the order of the numbers after the use of partition function? Assume we are sorting in increasing order. 11, 15, 9, 13, 17, 7, 5, 12, 6, 18

2 Answers

1 1 vote

array[ ] = { 11, 15, 9, 13, 17, 7, 5, 12, 6, 18 }

indexes :    0    1   2   3    4   5  6   7   8    9

pivot = array[0] = 11; i = 0 and j = 1

we traverse the entire array from left to right ie from j=1 to j=9 -

  1. array[ j ] > pivot ie 15 > 11 → j++
  2. array[ j ] < pivot ie 9 < 11 → i++ then swap array[ i ] and array[ j ] then j++. Now, array[ ] = { 11, 9, 15, 13, 17, 7, 5, 12, 6, 18 }
  3. array[ j ] > pivot ie 13 > 11 → j++
  4. array[ j ] > pivot ie 17 > 11 → j++
  5. array[ j ] < pivot ie 7 < 11 → i++ then swap array[ i ] and array[ j ] then j++. Now, array[ ] = { 11, 9, 7, 13, 17, 15, 5, 12, 6, 18 }
  6. array[ j ] < pivot ie 5 < 11 → i++ then swap array[ i ] and array[ j ] then j++. Now, array[ ] = { 11, 9, 7, 5, 17, 15, 13, 12, 6, 18 }
  7. array[ j ] > pivot ie 12 > 11 → j++
  8. array[ j ] < pivot ie 6 < 11 → i++ then swap array[ i ] and array[ j ] then j++. Now, array[ ] = { 11, 9, 7, 5, 6, 15, 13, 12, 17, 18 }
  9. array[ j ] > pivot ie 18 > 11 → j++

swap pivot and array[ i ]. Now, array[ ] = { 6, 9, 7, 5, 11, 15, 13, 12, 17, 18 }

0 0 votes
I am getting answer as

6,7,9,5,11,15,13,17,12,18

But it is not matching with the options given in the question.
Position:
Show:

Related questions

2 2 votes
1 1 answer
188
188 views
GO Classes asked Aug 31
188 views
During sorting, one complete processing of all elements whose final positions have not yet been determined is called a pass.Which of the following sequences cannot be the...
1 1 vote
1 1 answer
139
139 views
GO Classes asked Aug 10
139 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...
0 0 votes
1 1 answer
131
131 views
GO Classes asked Aug 10
131 views
Consider the Quick sort algorithm which sorts elements in ascending order using the first element as pivot. Then which of the following input sequence will require a maxi...
0 0 votes
1 1 answer
157
157 views
GO Classes asked Aug 8
157 views
The best case behaviour occurs for quick sort is, if partition splits the array of size $n$ into$n/2:(n/2)-1$ $n/2:n/3$ $n/4:3n/2$ $n/4:3n/4$