|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contact Us | Privacy Policy | Terms and Conditions | About
©2006 SparkNotes LLC, All Rights Reserved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||