Selection Sort
Selection Sort
SELECTION SORT
• If bubble sort was to be performed on an
bookshelf of books out of order, then comparing
each neighbouring books and then swapping
them over would be an expensive operation as
you would be taking a book out multiple times
depending on the number of swaps and possible
passes.
• The cost of moving items is relatively large
against the cost of comparing items.
Selection Sort
• Scan through all the elements find the smallest
element and then swap it with the element in the
left most position.
• Scan through remaining elements (N-1) finds the
smallest amongst them and then put that
smallest element to the right of the 1 st smallest
element. This process goes on finding the
smallest element to the right of those already
sorted and then doing a swap if necessary. In
this sort each element is moved directly to its
final position rather that taking small steps to the
final position.
first find the smallest in the array and exchange it with the element in
the first position, then find the second smallest element and exchange
it with the element in the second position, and continue in this way until
the entire array is sorted
10 12 2 9 4 6 5 3 1
1 12 2 9 4 6 5 3 10
1 2 12 9 4 6 5 3 10
1 2 3 9 4 6 5 12 10
1 2 3 4 9 6 5 12 10
1 2 3 4 5 6 9 12 10
1 2 3 4 5 6 9 10 12
THE END