Selection Sort

Selection Sort

Procedure:
     Step 1: select the smallest data item from index position 1 to n, then interchange the element with the 1st index element. So that the smallest element is available in the first position.

    Step 2: In the second pass, select the next smallest element from index position 2 to n, and interchange the element with 2nd index element. So that the second smallest element is available in second position.

   Step 3: The above procedure is repeated for n-1 passes. At the end of the n-1th pass, all elements of the array are available in sorted order.

Algorithm:  
            Step 1: Repeat i ß0 to n-1 and increment by 1
                                    Repeat j ßi+1 to n-1 and increment by 1
                                                if  a[i] > a[j]  then
                                                            temp = a[i];
                                                            a[i]    = a[j];
                                                            a[j] = temp;
                                                end if
                                    End Repeat
                          End Repeat

Example:

Before sorting the original array is a[5]={20,12,10,15,2}


Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment