Selection sort is a simple method of sorting.Arranging data in ascending order.Just by interchanging of data, which is data X > Y then interchange will takes place.

Algorithm :


Function selection_sort(a, n)
a: list of numbers (array)
n: size of the list.

Step 1: entry
Step 2: for I from 0 to n-2 do the following steps.
             Begin
Step 3: for j from i+1 to n-1 do the following steps.
Begin
Step 4: if a[i] > a[j] then swap ithelement and jthelement of array a.
end
end
Step 5: exit

Example  
A[0]             A[1]         A[2]          A[3]           A[4]
25
15
30
10
1


25
15
30
10
1


    Comparing 25 and 15, 25>15 interchange 15and 25

15
25
30
10
1

Compare 15 with 25,30, 10. 15>10 so interchange


10
25
30
15
1

now 10 is comparing with 25,30,15,and1.10>1 interchange


1
15
30
10
25

1 is least data among all,so is placed at A[0] location and it completes First Iteration


1
15
30
10
25

Now comparing 15with 30,10.15>10 interchange


1
10
30
15
25

Now comparing 10 with 30,15,25 no interchange ALL>10



1
10
30
15
25


It shows that completion of Second Iteration

1
10
30
15
25

Now compare 30 with 15. 30 >15 interchange


1
10
15
30
25

 comparing 15 with 30 and 25,15<30,25 no need of interchange.It Completes Third Iteration


1
10
15
30
25

now 30 compared with 25, 30>25 interchange


1
10
15
25
30

 Data is now in sorted order and completes Fourth Iteration

NOTE:In every iteration smallest data is placed in the right location/position

Posted by Unknown On 04:48 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *