Data Structures & AlgorithmsVery hard
Selection sort (ascending) is run on 7 2 9 4 1 6, swapping the minimum of the unsorted part into place in each pass, and skipping the swap when the minimum is already in place. How many swaps are performed?
- A. 5
- B. 3
- C. 4
- D. 9
Correct answer: B. 3
Selection sort performs at most n−1 swaps, and only when the chosen minimum is not already at the front of the unsorted part. Counting inversions measures a different quantity, and the n−1 bound is reached only in special cases.
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
- Sort 7 8 3 12 5 6 15 4 by the NUMBER OF 1-BITS in binary, ascending; values with the same bit count are ordered by value ascending. Give…
- A queue holds 1 2 3 4 5 6 7 8 from front to back. Interleave it: take the front element of the first half, then the front of the second…
- Starting from an empty stack, perform in order: push 4, push 9, pop, push 2, push 7, pop, pop, push 5. Reading from the bottom to the top…
- Take the values of 4 9 2 7 3 8 5 6 1 10 at the ODD 0-based indices (1, 3, 5, …) in order, forming a new sequence. What is the length of…
- Split 8 3 5 1 9 2 7 4 into consecutive blocks of 4 (positions 0–3 and 4–7). For each block take (max − min), and add these two numbers…
- In 3 1 4 1 5 9 2 6 5 3 5 8, count how many positions hold a NEW strict prefix maximum (the element is strictly greater than every element…