Selection Sort
Selection Sort
Selection Sort
SELECTION SORT
This sorting technique begins by comparing first element in the given array
of elements with the rest of the elements to find smallest element and then
that is exchanged with the first element.
Then the process is repeated with the second element to find second smallest
element and is exchanged with the second element and so on.
Finally , all elements will be arranged in ascending order.
Since , the next least element is selected and exchanged appropriately so that
elements are finally sorted, this technique is called Selection sort.
Eg: Consider following array A as below and sort it using selection sort.
A[0] A[1] A[2] A[3] A[4]
A 45 20 40 5 15
Begin by assuming that the first smallest element is in The position 0 i.e, small=A[0] and
pos=0. Then as smaller element is found assign that to small and its position to pos.
45 20 40 5 15
A[1]<small , small=20, pos=1.
45 20 40 5 15
A[2]>small, small=20,pos=1
45 20 40 5 15
Pass I
45 20 40 5 15
A[3]<small, small=5, pos=3
Begin with small=20 and pos=1
5 20 40 45 15
A[2]>small, small=20 and pos=1
5 20 40 45 15
Pass II
5 20
A[3]>small, small=2040 45
and pos=1 15
Initially
5 small=40
15 and40
pos=2 45 20