GATE Computer ScienceVery hardGATE CSE · NIELIT
Lomuto partition (pivot = last element, i starts at −1, for each j with A[j] ≤ pivot do i++ and swap A[i], A[j]; finally swap A[i+1] with the pivot) is applied to [8, 3, 10, 1, 7, 12, 5, 9, 2, 6]. Counting every executed swap statement, including swaps of an element with itself, how many swaps occur, and what is the final index of the pivot?
- A. 5 swaps, pivot at index 4
- B. 4 swaps, pivot at index 4
- C. 5 swaps, pivot at index 3
- D. 6 swaps, pivot at index 5
Correct answer: A. 5 swaps, pivot at index 4
Elements ≤ 6 are 3,1,5,2 giving 4 in-loop swaps, plus 1 final swap = 5; the pivot lands at index 4 (final array 3 1 5 2 6 12 10 9 8 7).
More Algorithms questions
- A max-heap is built from the array [3, 9, 2, 1, 4, 5, 8, 7, 6, 10] using the bottom-up procedure (sift-down from index ⌊n/2⌋−1 to 0…
- A hash table of size 11 uses double hashing with h1(k) = k mod 11 and h2(k) = 7 − (k mod 7), probing slot (h1 + i·h2) mod 11 for i = 0, 1…
- How many distinct spanning trees does the complete bipartite graph K(3,4) have?
- Floyd–Warshall is run on a 5-vertex digraph (vertices 0..4) with arc weights 0→1:3, 0→3:7, 1→0:8, 1→2:2, 2→0:5, 2→3:1, 3→0:2, 3→4:4 (no…
- In how many ways can a longest strictly increasing subsequence be chosen (counting index sets, not distinct value sequences) from the…
- Randomised quicksort picks the pivot uniformly at random from the current sub-array and each partition of m elements uses exactly m−1…