Selection sort practice problem

Selection sort is an algorithm to arrange the elements of a data structure, such as an array or list, in increasing or decreasing order. The algorithm partitions the array into sorted and unsorted parts and repeatedly finds the minimum (or maximum) in the unsorted part and swaps it with the last element of the sorted part.

The AP CS Elevens Lab features a shuffle method that is similar to selection sort.

On the AP Computer Science Exam, you must recognize the algorithm when it is presented in code. You may be expected to compare the efficiency to insertion sort. You must also be able to recognize a variation of the algorithm such as sorting in decreasing order or sorting from back to front.

Selection sort trace

Show each step as selection sort is run on the array below.

[71 86 79 36 78 35 75 86 24 11]

At each step, separate the sorted and unsorted parts with the | symbol.

Selection sort implementation

Method sort arranges the elements in x in increasing order using the selection sort algorithm.

Complete method sort below. You may also declare and implement a swap method with the header of your choice.

public static void sort(int[] x)

See the Selection sort practice problem solution or review it with AP CS Tutor Brandon Horn.

Get AP CS Help