Selection sort as the name suggests, first select the least element from the entire list in every iteration and swap it with the position for which iteration is being performed.
In the given figure it is seen that in the first iteration the first and second element is compared the smaller value is placed at the first position , after this in the second and third element is compared and so on .
Algorithm for Selection Sort
Step 1 Set MIN to location 0
Step 2 Search the minimum element in the list
Step 3 Swap with value at location MIN
Step 4 Increment MIN to point to next element
Step 5 Repeat until list is sorted
It is clearly seen from the algorithm that the location is compared one by one in every iteration and swapping is been done.
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
/* check the element to be minimum */
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
/* swap the minimum element with the current element*/
if indexMin != i then
swap list[min] and list[i]
end if
end for
end procedure
0 Comment(s)