The first two items in [1, 2, 7, 8, 4, 3, 6] are sorted. 3 is the smallest item in the unsorted portion ([7, 8, 4, 3, 6]). Swapping 3 with 7, we have [1, 2, 3, 7, 4, 7, 6] and the first three items are sorted.

Selection Sort Algorithm

Quick reference

Complexity
Worst case time
Best case time
Average case time
Space

Strengths:

  • Intuitive. Ever packed a suitcase, putting in large items before smaller ones? That's selection sort!
  • Space efficient. Selection sort only requires a constant amount of additional space ().

Weaknesses:

  • Slow. Selection sort takes time, even if the input is already sorted. That's too slow to be used on super-big data sets.

How It Works

Selection sort works by selecting the smallest element from an unsorted array and moving it to the front.

Here's how it'd work on this array:

Unsorted list: [8, 3, 2, 7, 9, 1, 4]

We'll scan through all the items (from left to right) to find the smallest one ...

Scanning through [8, 2, 3, 7, 9, 1, 4], the smallest element is 1.

... and move it to the front.

Swapping 1 with the first element (8), we have [1, 3, 2, 7, 9, 8, 4].

Now, the first item is sorted, and the rest of the array is unsorted.

In [1, 3, 2, 7, 9, 8, 4], [1] is sorted and [3, 2, 7, 9, 8, 4] is unsorted.

Repeat! We'll look through all the unsorted items, find the smallest one, and swap it with the first unsorted item.

[1] is sorted and [3, 2, 7, 9, 8, 4] is unsorted. Scanning through the unsorted items [3, 2, 7, 9, 8, 4], 2 is the smallest. Swapping 2 with the first unsorted item, 3, we have [1, 2] as the sorted portion and [3, 7, 9, 8, 4] as the unsorted portion.

We can do this for the next-smallest item

[1, 2] is sorted and [3, 7, 9, 8, 4] is unsorted. Scanning through the unsorted items [3, 7, 9, 8, 4], 3 is the smallest. It's already the first unsorted item, so we don't have to move it. [1, 2, 3] is the sorted portion and [7, 9, 8, 4] is the unsorted portion.

And so on:

[1, 2, 3] is sorted and [7, 9, 8, 4] is unsorted. Scanning through the unsorted items [7, 9, 8, 4], 4 is the smallest. Swapping 4 with the first unsorted item, 7, we have [1, 2, 3, 4] as the sorted portion and [9, 8, 7] as the unsorted portion.
[1, 2, 3, 4] is sorted and [9, 8, 7] is unsorted. Scanning through the unsorted items [9, 8, 7], 7 is the smallest. Swapping 7 with the first unsorted item, 9, we have [1, 2, 3, 4, 7] as the sorted portion and [8, 9] as the unsorted portion.
[1, 2, 3, 4, 7] is sorted and [8, 9] is unsorted. Scanning through the unsorted items [8, 9], 8 is the smallest. 8 is already the first unsorted item, so we don't need to move it. [1, 2, 3, 4, 7, 8] is the sorted portion and [9] is the unsorted portion.
Scanning through the unsorted items [9], 9 is the smallest (and only) item. It's already the first unsorted item, so we don't need to move it. The whole list is sorted: [1, 2, 3, 4, 7, 8, 9].

And, we're done!

The final sorted list: [1, 2, 3, 4, 7, 8, 9].

Implementation

Here's how we'd code it up:

public static void selectionSort(int[] theArray) { for (int i = 0; i < theArray.length; i++) { // run through the unsorted elements in the input, finding // the smallest one. int smallestIndex = i; for (int j = i + 1; j < theArray.length; j++) { if (theArray[j] < theArray[smallestIndex]) { smallestIndex = j; } } // move the smallest element to the front of the unsorted portion // of the input (swapping with whatever's already there). int tmp = theArray[smallestIndex]; theArray[smallestIndex] = theArray[i]; theArray[i] = tmp; } }

Complexity

Selection sort takes time and space.

The main time cost comes from scanning through the array to find the next smallest item. We do that n times. The first time, we'll be looking at n elements, the next time it'll be n - 1 elements, and so on, until we're left with just one element.

Adding all those up, we've got

n + (n - 1) + (n - 2) + \ldots + 2 + 1

That's the triangular series, which is .

Even if the input is already sorted, selection sort still involves scanning all the unsorted elements repeatedly to find the next-smallest. So, unlike insertion sort, it'll stay , even in the best case.

Selection sort doesn't rely on any extra arrays, so it's space.

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .