GATE Computer ScienceModerateGATE CS (PYQ pattern)
Cocktail-shaker sort is applied to [5, 1, 4, 2, 8, 0, 3]: sweeps alternate left-to-right and right-to-left over the unsorted part (each completed sweep fixes one more end element) and the algorithm stops after the first sweep that makes no exchange. How many sweeps are executed, including that final one?
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
Sweep 1 (→): [1,4,2,5,0,3,8]. Sweep 2 (←): [0,1,4,2,5,3,8]. Sweep 3 (→): [0,1,2,4,3,5,8]. Sweep 4 (←): [0,1,2,3,4,5,8]. Sweep 5 (→) makes no exchange, so 5 sweeps. Plain bubble sort with early exit needs 6 passes here, because the small key 0 moves left only one place per pass.
More Algorithms questions
- In randomised quicksort on n = 10 distinct keys (pivot chosen uniformly, so two keys with ranks i < j are compared with probability 2/(j −…
- Multiplying two n × n matrices classically costs n³ multiplications and n²(n − 1) additions. One level of Strassen (7 products of n/2 ×…
- Karatsuba multiplies two n-digit numbers (n = 2^k) with 3 half-size products, recursing down to 1-digit numbers; the classical method uses…
- Let T(1) = 1 and T(n) = (1 + 1/n)·T(n − 1) + 1 for n ≥ 2. Which is the tight bound on T(n)?
- The running time satisfies T(n) = T(n/2) + T(n/3) + T(n/6) + n. Which bound is tight?
- Solve T(n) = √n·T(√n) + n·log₂ n for n = 2^(2^k), with T(2) = 2. Which is the tight bound?