As mentioned, the selection sort algorithm swaps the element in the first position of the unsorted sub-list with the element with the lowest value in the remainder of the unsorted sub-list.

How does this algorithm swap these elements in practice? Let’s say we have the two values stored at the following indices index_1 and index_2. How would we swap these two elements within the list?

It is tempting to write code like:

array[index_1] = array[index_2]; array[index_2] = array[index_1];

However, if we do this, we lose the original value at index1. The element gets replaced by the value at index2. Both indices end up with the value at index2.

Programming languages have different ways of avoiding this issue. In some languages, we create a temporary variable which holds one element during the swap:

int temp = array[index_1]; array[index_1] = array[index_2]; array[index_2] = temp;

This animation illustrates how the swapping in the selection sort algorithm should work:

Using the temporary variable strategy seen in the GIF above avoids the loss of any of the values whose position we need to exchange.

We’ll use this strategy to build out swap().



Look at the swap() method. Notice that swap() takes 3 arguments: arr, the input array, and indexOne, and indexTwo, the two indices whose values we want to swap.

In the method, create an int variable called temp and store the value of the element at the indexTwo position in the input array so it can be referenced later.


Change the value at indexTwo of the input array to the value of the element at indexOne.


Set the element at indexOne of the array to the original value of the element at indexTwo by referring to the temp variable we created earlier (the element at indexTwo is no longer the same after the previous step).


In the main() method, let’s test out the swap method. Add the following code to the main() method:

int[] swapData = {-10, 15, 18, 10, 25}; // provide arguments for `swap()` SelectionSort.swap();

Check out the array called swapData. Call swap() so that the values 15 and 10 swap places with each other. We will print the results in our next checkpoint!


Add a print statement in the main method right after the swap() call. To print an array use System.out.println(Arrays.toString(arrayName)). Make sure to use the correct variable name of your array.


Now that we know our swap() method works, let’s add it to our selectionSort() method.

Update the code executed in our main() method to:

int[] data = { 2, 7, -7, 20, -5 }; SelectionSort.selectionSort(data); System.out.println("Sorted Array in Ascending Order: "); System.out.println(Arrays.toString(data));

Next, in our selectionSort() method, after our inner for loop but before we close our outer for loop, call the swap() method passing in arr, i and currentMinimumIndex.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?