Data Structures & AlgorithmsModerateGATE CS · competitive programming
What is the minimum number of adjacent swaps needed to sort [3, 1, 2, 5, 4] into ascending order?
- A. 2
- B. 4
- C. 5
- D. 3
Correct answer: D. 3
Each adjacent swap removes exactly one inversion. This array has three: (3,1), (3,2) and (5,4), so three swaps is the minimum.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- One Lomuto partition pass with the last element as pivot runs on [3, 8, 2, 5, 1, 4, 7, 6]. What is the array after this pass, before any…
- A three-way partition (Dutch national flag) with a low pointer, a mid pointer and a high pointer runs on [2, 0, 2, 1, 1, 0]. What is the…
- What is the smallest positive integer missing from the array [3, 4, −1, 1]?
- What is the worst-case time complexity of finding the minimum in a rotated sorted array that may contain duplicates, using a binary search…
- For binary search over 1000 distinct sorted values with mid = (lo + hi) / 2 and the loop running while lo ≤ hi, what is the largest number…
- In a Fenwick tree with n = 16, an update at index 5 moves i ← i + (i & −i) while i ≤ n. How many tree nodes does the update touch?