Foundations-of-Computer-Science practice question 3 of 15

Which action is taken if the first number is the lowest value in a selection sort?

Full set: 71 questions
Answer options
A:

A. The first number is increased by one.

B:

B. The first number is duplicated.

C:

C. It swaps the selected element with the last unsorted element.

D:

D. It swaps the selected element with the first unsorted element.

Correct answer
Correct Answer:
D. D. It swaps the selected element with the first unsorted element.
Explanation

The selection sort algorithm works by repeatedly finding the minimum element from the unsorted part of the list and placing it at the beginning of that unsorted part. In each pass, it identifies the minimum value and swaps it with the element at the current position (the first unsorted element). If the element at the current position is already the minimum, the algorithm still performs the swap operation, effectively swapping the element with itself. This ensures the algorithm's logic remains consistent regardless of the data's initial order.

Why Incorrect

A. The first number is increased by one: This is not an operation performed by the selection sort algorithm.
B. The first number is duplicated: Duplication is not a part of the selection sort algorithm's logic.
C. It swaps the selected element with the last unsorted element: This describes a different sorting logic, not selection sort.

References

1. Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to Algorithms (4th ed.). MIT Press. Chapter 2, "Getting Started," Problem 2-2, describes the logic of selection sort.

2. MIT OpenCourseWare. (2011). 6.006 Introduction to Algorithms, Fall 2011. Lecture 3: Sorting I. This lecture describes the mechanics of selection sort, including the invariant that the minimum element is swapped with the first element of the unsorted partition.

Scroll to Top