touque.ca > Education Commons > Java > Resources > Algorithms

Selection Sort

The idea behind selection sort is that to sort N items you make N passes through them.

On the first pass you find the largest value, and then swap it with the last item. Thus the largest item is now in position N.

On the second pass you scan through just the first N-1 entries. The largest of those items is swapped with the item in position N-1. Hence the largest item overall is now in the last position; the second largest item is now in the second last position.

This process is repeated, with one item being placed in its correct location each time.

After N passes, the entire collection of data is sorted.

A simple variation is to find the smallest item each time and put it at the front.

To sort into descending order, the largest item is found each time and moved to the front.

(Abridged from

touque.ca > Education Commons > Java > Resources > Algorithms

[This page last updated 2020-12-23 at 12h12 Toronto local time.]