Select Sort


Selection Sort

The Selection Sort is a very basic sort. It works by finding the smallest element in the array and putting it at the beginning of the list and then repeating that process on the unsorted remainder of the data. Rather than making successive swaps with adjacent elements like bubble sort, selection sort makes only one, swapping the smallest number with the number occupying its correct position.

Consider the following unsorted data: 8 9 3 5 6 4 2 1 7 0. On the first iteration of the sort, the minimum data point is found by searching through all the data; in this case, the minimum value is 0. That value is then put into its correct place at the beginning of the list by exchanging the places of the two values. The 0 is swapped into the 8's position and the 8 is placed where the 0 was, without distinguishing whether that is the correct place for it, which it is not.

Now that the first element is sorted, it never has to be considered again. So, although the current state of the data set is 0 9 3 5 6 4 2 1 7 8, the 0 is no longer considered, and the selection sort repeats itself on the remainder of the unsorted data: 9 3 5 6 4 2 1 7 8.

Consider a trace of the insertion sort algorithm on a ten element data set:

8 9 3 5 6 4 2 1 7 0
0 9 3 5 6 4 2 1 7 8
0 1 3 5 6 4 2 9 7 8
0 1 2 5 6 4 3 9 7 8
0 1 2 3 6 4 5 9 7 8
0 1 2 3 4 6 5 9 7 8
0 1 2 3 4 5 6 9 7 8
0 1 2 3 4 5 6 9 7 8
0 1 2 3 4 5 6 7 9 8
0 1 2 3 4 5 6 7 8 9

Take a Study Break

SparkLife

Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

SparkLife

Are you afraid of relationships?

Auntie SparkNotes can help!

SparkLife

Wanna get JLaw's gorgeous glow?

Click here for simple, sexy makeup tricks!

SparkLife

Sexy starlet style

See every single look from the Met Gala!

SparkLife

Who'd be on your zombie-apocalypse crew?

We already dib'sed Genghis Khan.

Geek out!

The MindHut

Geeky Actors: Then and Now

Travel back in time!

The MindHut

Villains We Want These Actresses to Play

From super cute to super bad!

The MindHut

10 Movies Better Than Their Books

What do you think?

The MindHut

Summer Movie Open Thread

Leave your thoughts here!

The MindHut

12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?

The Book

Cover image

Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps