Wednesday, March 4, 2009

Selection Sort Method

The steps to sort data using selection sort method are

Consider A is an list / array of n elements

Step 1:  Find the postion of smallest element from A1 to An and then interchage the element of that location  with A1. 

Step 2: Find the postion of smallest element from A2 to An and then interchage the element of that location  with  A2.

Step 3: Find the postion of smallest element from A3 to An and then interchage the element of that location  with  A3.
.
.
.
Step n: Find the postion of smallest element from An to An and then interchage the element of that location  with  An.


Complexity
  • Worst Case: n(n-1)/2 = o(n2)
  • Average Case: n(n-1)/2 = o(n2)
  • Best Case: n(n-1)/2 = o(n2

Algorithm: Given A is an array of N elements. POS denoted the position of smallest element

1. Repeat thru step 4 for STEP=1 to N
2. POS=STEP
3. [for Asce order find position of smallest element]
    Repeat for i=STEP+1 to n
       if A[i]       then POS=i
4. [Swap]
     if POS<> step
     then  temp=a[setp]
              a[step]=a[POS]
              a[POS]=temp
5. Print Sorted Data
6. Exit

No comments:

Post a Comment

Learn Programming online

Started Two months Online Programming Classes for XI, XII (all boards), B.Sc.(CS/IT), BCA, M.Sc.(CS/IT), MCA. Programming Languages 1. C 2. ...