Searching, Sorting & Selection
![Question 49, GATE 2015: partition(a, n)moves first-element pivot to end of left part and prose says it returns left part COUNT. Skeleton sets p=partition(a, n); if(p+1==k)return a[p]; if(p+1>k)return kth_smallest(…); else return kth_smallest(…). Missing calls: A(a, p, k), (a+p+1, n−p−1, k−p−1); B(a, p, k), (a, n−p−1, k−p−1); C reverse A calls; D(a, n−p−1, k−p−1), (a, p, k). The fixed code instead requires pivot INDEX.](../../assets/questions/CS-2015-S2-Q49-1.webp)
Answer & solution
Official answer: A
Our answer: A only if partition returns zero-based pivot index
With pivot index p, its rank is p+1. The left recursive call uses(a, p, k); the right uses(a+p+1, n−p−1, k−p−1), giving A. However the prose explicitly says partition returns the left-part COUNT, which is p+1. The fixed skeleton then compares the wrong rank and accesses the wrong element; no option repairs that. Correct by assigning left_end=partition(a, n)−1 or changing the function contract to return the pivot index. Official A retains this index interpretation.
If p were the zero-based pivot index, its rank would be p+1. Recursing left keeps the same base pointer and k but changes length to p. Recursing right advances past the pivot byp+1 and subtracts that many elements from both length and rank. These are A’s arguments. But the prose-returned count is p+1 already; assigning it to p makes the fixed equality and a[p] off by one before either missing call runs. Thus no choice repairs the literal contract. A applies only after changing the contract or subtracting 1 on assignment.