GATE Computer ScienceVery hardGATE CSE
Quicksort with Lomuto partitioning (pivot = last element of the sub-array, loop compares A[j] ≤ pivot for j from lo to hi−1) sorts [4, 9, 2, 7, 1, 8, 3, 10, 5, 6]. How many element-versus-pivot comparisons (A[j] ≤ pivot) are made in total across all partition calls?
- A. 21
- B. 25
- C. 18
- D. 29
Correct answer: A. 21
Each partition of a sub-array of size m costs m−1 comparisons; following the recursion splits gives 21.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- The Hoare partition scheme is applied once to A = [5, 3, 8, 5, 1, 9, 5, 2, 7, 4] with pivot x = A[0] = 5, using: i = −1, j = 10; loop { do…
- A singly linked list holds 1, 2, …, 11 in order. Step 1: reverse it in consecutive groups of 4 nodes (a final group with fewer than 4…
- Infix a+bc^d^e-(f+g)h/i^j^k is converted to postfix with the usual stack algorithm: operators and "(" are pushed on a stack, an operator…
- A binary tree on keys 1 to 6 has preorder 1 2 3 4 5 6 and postorder 3 2 6 5 4 1. Treating a left child and a right child as different, how…
- An array int A[-2..3][4..9][1..5] (inclusive bounds, 4-byte ints) is stored in row-major order starting at byte address 1000. What is the…
- The non-zero elements of a 30 × 30 tridiagonal matrix (non-zeros only where |i − j| ≤ 1, indices 1-based) are stored row by row in a 1-D…