SELECTION SORT


This is the simplest sorting method. In order to sort data in ascending order, the 1st element is compared to all other elements and if found greater than the compared element, then they are interchanged. So after the first iteration the smallest element is placed at the 1st position. The same procedure is repeated for the 2nd element and so on for all the elements.

Let's take an example to sort a given list of numbers(27,19,33,15,4).


You must learn a few points before going through this iterative version of selection sort in an array. These are as follows:
The elements marked as yellow in any row are those that are compared at a particular time instant.The elements that are marked with green are those elements that had already been sorted until the starting of the iteration or left out as sorted until that particular iteration ends.

First Iteration

 

Num1Num2 Num3 Num4 Num5
27 19 33 15 4
19 27 33 15 4
19 27 33 15 4
15 27 33 19 4 4 27 33 19 15

Second Iteration

Num1Num2 Num3 Num4 Num5
4 2733 19 15
4 27 33 19 15
4 19 33 27 15
4 15 33 27 19

Third Iteration

Num1Num2 Num3 Num4 Num5
4 15 33 27 19
4 15 27 33 19
4 15 19 33 27


Fourth Iteration

Num1Num2 Num3 Num4 Num5
4 15 19 33 27
4 15 19 2733

this is the iteration wise presentation of sorting step by step

This is the best way to understand this topic.

Pseudo Code

START
SET l=size of array
For(i=0;i
FOR(j=i;j
If (A[i]>A[j])
Swap(A[i],A[j])
End if statement
End for loop
End for loop
END

 

COMPLEXITY

CASEBEST CASE
AVERAGE CASE
WORST CASE
COMPLEXITY O(square of n) O(square of n) O(square of n)

 

These are very general pseudo codes which helps a lot to develop the actual working C/c++ codes . For exact code you can refer to this link - click here

Non-profit Tax ID # 203478467