How do you sort a list in Java? You’ve got a few options. A selection sort is a particularly common method that is used to sort a list.

A selection sort repeatedly finds the smallest element in a list and moves it to the beginning of the unsorted part of the list. This process repeats until a list is ordered.

In this guide, we’re going to talk about what selection sorts are and how they work. We’ll also walk through how to build a selection sort in Java so that you’ll know how to build your own. Let’s get started!

## What is a Java Selection Sort?

A selection sort repeatedly finds the minimum item in a list and moves it to the beginning of the unsorted items in the list. This process is repeated for every item in a list until the list is ordered.

**Take this quiz to get offers and scholarships from top bootcamps and online schools!**

The first item in the list is considered to be the smallest item. This item is then compared with the next element. If the next element is smaller, they are swapped. This algorithm finds the minimum element until the last element is reached. Then, the smallest item is moved to the start of the list.

In a selection sort, a list is divided into two parts:

- The sorted list
- The unsorted list

As elements are sorted, they move from the unsorted list to the sorted subarray.

If a list is still not ordered, this process will continue until all elements are in their correct positions.

Selection sorts can order items in either ascending or descending order.

## When Should You Use a Selection Sort?

Selection sorts are optimal when you need to sort a small list. This is because there are more efficient ways of sorting large lists. Algorithms, such as a merge sort, an insertion sort, and a quick sort, are more efficient than a selection sort in Java programming.

A selection sort performs best when checking all of the elements of the array is mandatory. This would be the case if few or none of the items in a list are sorted. Selection sorts usually outperform a bubble sort, which is the most simple sorting algorithm.

## How do Selection Sorts Work?

There’s no use trying to implement an algorithm in Java without first knowing what it is that we want our algorithm to do. Let’s start by walking through the steps a selection sort takes to sort a list in order.

Consider the following unsorted array:

17 | 14 | 9 | 12 |

Selection sorts set the first item as the smallest in the list. This is a temporary value which changes every time a comparison is made. This value is stored in its own variable.

minimum = 17 | |||

17 | 14 | 9 | 12 |

The “minimum” item is then compared with the second element. If the second element is smaller than the “minimum” item, the value of the “minimum” item will be set to the value of the second item. 14 is smaller than 17, so our new minimum value becomes 14.

minimum = 14 | |||

17 | 14 | 9 | 12 |

This process repeats for each item in our list. 9 is less than 14, so the value of “minimum” becomes 9. 9 is not less than 12, so the value of minimum stays the same.

After one iteration, our list has found that 9 is the smallest number. This item is then moved to the start of the list:

9 | 17 | 14 | 12 |

This process starts over again from the first unsorted element. So, our next set of comparisons would begin with 17:

- 17 is set as the minimum.
- 17 is compared with 14. The value of “minimum” is set to 14.
- 14 is compared with 12. The value of “minimum” is set to 12.
- 12 is moved to the end of the sorted items in the list.

Our list now looks like this:

9 | 12 | 17 | 14 |

This process repeats until our list is ordered. When our algorithm has finished executing, the following list is returned:

9 | 12 | 14 | 17 |

Our list is now sorted in ascending order.

## How to Build a Selection Sort in Java

It’s one thing to know how a selection sort works; it’s another to build one. Let’s code a selection sort in Java that uses the logic we discussed in the walk through.

Create a file called selection_sort.java. We’ll start by importing the Arrays library into our code:

`import java.util.Arrays;`

This library is used later in our code to convert our sorted array to a string so that we can print it to the console. Next, we’re going to declare a class and create a method which performs our selection sort. Add the following to your `selection_sort.java`

file:

class SelectionSort { void sortNumbers(int array[]) { int size = array.length; for (int item = 0; item < size - 1; item++) { int minimum = item; for (int number = minimum + 1; number < size; number++) { if (array[number] < array[minimum]) { minimum = number; } } int temporary = array[item]; array[item] = array[minimum]; array[minimum] = temporary; } } }

In our class, we have defined a method called `sortNumbers`

which performs our sort. We start by calculating the length of our array. Then, we create a for loop which iterates through every item in our list. Inside this for loop, we find the minimum item, which is the first item in the list.

We then start another for loop to compare the minimum item with every item in the list. If the number the for loop is reading is smaller than the minimum number, the value of “minimum” becomes that number. In our loop, “number” represents the index value of the number to which we are comparing to the minimum value.

Once the minimum number has been compared to every number in the list, our inner for loop stops. The minimum number is then moved after all the sorted numbers in the list.

Our code doesn’t do anything just yet. We haven’t yet called our class and given it a list to sort.

Below the `sortNumbers`

method in the list, add the following code:

public static void main(String args[]) { int[] toSort = { 17, 14, 9, 12 }; SelectionSort newSort = new SelectionSort(); newSort.sortNumbers(toSort); System.out.println(Arrays.toString(toSort)); }

Inside our main method we have declared a list of items to sort called `toSort`

. We then initialize an instance of our SelectionSort class called newSort. We use this to call our `sortNumbers`

method, which sorts the values in the toSort array.

After the sortNumbers method has executed, we print out the sorted array to the console. We do this using the `Arrays.toString()`

method, which converts our array to a list of strings.

Let’s run our code:

[9, 12, 14, 17]

Our list has been sorted!

It’s worth noting that you can sort values in descending order. To do so, replace the following line of code in your `sortNumbers`

method:

`if (array[number] < array[minimum]) {`

With this code:

`if (array[number] > array[minimum]) {`

This will see whether the “minimum” value – which is technically the “maximum” value when you are sorting in descending order – is greater than the one being accessed by the for loop. This means that the value of “minimum” will reflect the highest value in a list instead of the lowest value.

To prevent confusion, you should rename “minimum” to “maximum”, if you are sorting a list in descending order.

You’ve done it. You have sorted a list in Java using the selection sorting algorithm.

## Complexity Breakdown

There are three time complexities that we need to consider when evaluating an algorithm: best case, worst case, and average case.

The selection sort has a best, average, and worst case complexity of O(n^2). This means that the algorithm will take exponentially longer as the numbers of the items in a list grow.

## Conclusion

Selection sorts are an efficient method of sorting lists of data. They work by selecting the smallest item from an unsorted list and moving that item to the beginning of the unsorted list. This process repeats until the list is sorted.

Now you’re ready to build your own selection sort in Java like an expert!