GATE Computer ScienceHardGATE CS (PYQ pattern)
For the array [8, 3, 10, 1, 7, 5, 12, 2, 9, 4, 6], let X be the number of exchanges bubble sort performs and Y the minimum number of swaps of arbitrary pairs of elements needed to sort it. What is X − Y?
- A. 20
- B. 25
- C. 19
- D. 28
Correct answer: A. 20
Bubble sort performs one exchange per inversion: X = 28. Y = n − (number of cycles of the permutation) = 11 − 3 = 8. So X − Y = 20. Subtracting the number of cycles instead gives 25.
More Algorithms questions
- A queue is implemented by two stacks (enqueue pushes onto IN; dequeue pops from OUT, first moving all elements of IN to OUT if OUT is…
- Keys 15, 9, 12, 4, 18, 7, 3, 10, 21, 6 are inserted one by one into an empty binary min-heap (array representation, standard sift-up)…
- Floyd's Build-Heap performs at most height(v) exchanges while sifting down node v, so its exchanges are bounded by H = Σ height(v) over…
- A 3-ary min-heap stores 1000 keys in an array A[0..999], with the children of A[i] at 3i + 1, 3i + 2 and 3i + 3. How many of the keys are…
- Computing x¹²⁷ by multiplication, where each step multiplies two powers already computed (squarings count as multiplications), what is the…
- Euclid's algorithm gcd(a, b) repeatedly replaces (a, b) by (b, a mod b) until b = 0, counting one step per mod operation. Over all pairs 1…