| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
About Exam
hard Solved by 1 students
Selection Sort Swapping Strategy
< Prev
Next >

What is the effect of the swapping strategy used in the above selection sort implementation compared to a classical selection sort?

public class OptimizedSelectionSort {
    public static void selectionSort(int[] arr) {
         for (int i = 0; i < arr.length - 1; i++) {
             int j = i + 1;
             while (j < arr.length) {
                 if (arr[i] > arr[j]) {
                     int temp = arr[i];
                     arr[i] = arr[j];
                     arr[j] = temp;
                 }
                 j++;
             }
         }
    }
}
A

It enhances stability by avoiding unnecessary swaps between equal elements.

B

It reduces overall swap operations by pre-sorting the elements before the main loop.

C

It results in multiple swaps per pass, which can decrease efficiency compared to the single swap in a classical selection sort.

D

It has no impact on the algorithm’s performance and behaves identically to a standard selection sort.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy