Wednesday 4 January 2017


Selection Sort : Java Program

Selection Sort is one of sorting techniques to get arranged the numbers in ascending order. 

Selection Sort method:

We will take the following list of elements.

10  30  15  6  4  1  8

Logic for finding the biggest number from the list:

Here we have 7 elements to be sorted.  In the selection sort method, we will pick up biggest element from the list.  For this we assume that first element is biggest number. Then compare the first number with the second number in the list.  Here 10 is the first number and 30 is the second number. From these two 30 is the biggest number. Now we can treat 30 is the biggest number. Go for comparison with the biggest number and next number 15. Again we get 30 is the biggest number. Now compare biggest number 30 with the next number 6. Among these two 30 is the biggest number. Compare this biggest number with the next number 4. Now compare 30 with the next number 1. Again we get 30 is the bigger number. Now compare 30 with the last number 8. We get the 30 is the largest number from the list.

The whole paragraph above gives that, finding of largest number from the given list.

In the above list 30 is the biggest number and it is located at 2 position. After finding the biggest number from the list,  we will change the positions of biggest number with the last number. That is we have to change positions of 30 and 8. After exchanging the positions,  the list look like this

10  8  15  6  4  1  30  -------> I Pass

This is called first pass. During the first pass we can get one element as sorted.  Apply the same logic to the remaining 6 elements.

Among the 6 elements 15 is the biggest element.  Hence we have to swap the positions of biggest number 15 and last number 1. After doing this, the list look like this.

10  8  1  6  4  15  30  --------> II Pass

This is called second pass.

Apply the same logic for the remaining 5 elements. The III Pass looks like this after swapping 10 and 4 positions

4  8  1  6  10  15  30 --------> III Pass

Among the remaining four elements, 8 is the biggest number and 6 is the last number. Therefore, we have to exchange these positions we get the following the IV pass.

4  6  1  8  10  15  30 ---------à IV Pass

We have here 3 elements remaining. Among these 6 is the largest one and last number is 1. We can swap these elements to get the V Pass and it looks like the following

4  1  6  8  10  15  30 ---------à V Pass

We have here 4 is the largest element and 1 is the last element. So we have swap these two to get the VI Pass. The list looks like this.

1  4  6  8  10  15  30 --------------à VI Pass

You can observe from the VI Pass, all the elements got arranged in the ascending order.
If elements are 7, then 7-1=6 passes require to sort the numbers in ascending order.  In general, if n elements are in the list, then n-1 passes are required to get the numbers in sorted order.

This type of sorting technique is called selection sort method.


Java Program for Selection Sort:

class Sele
{
       public static void main(String args[])
       {
           int a[]={10,30,15,6,4,1,8}; 

           int i,b,id,j,n;
           n=a.length;
           System.out.println(" Elements before sorting");
           for(i=0;i<n;i++)
             System.out.print(a[i] + " ");
           for(i=n-1;i>=0;i--)
            {
                 b=a[0];
                 id=0;
                 for(j=1;j<i+1;j++)
                   {
                        if(a[j]>b)
                          {
                               b=a[j];
                               id=j;
                          }
                    }
                a[id]=a[i];
                a[i]=b;
           }
         System.out.println();
         System.out.println("Elements after sorting");
         for(i=0;i<n;i++)
          System.out.print(a[i] + " ");
      }
}
output:

 Elements before sorting
10  30  15  6  4  1  8

Elements after sorting

1  4  6  8  10  15  30