Search This Blog

Wednesday, January 30, 2013

SELECTION SORT

import java.io.*;
class selectionsort
{
public static void main(String args[])throws IOException
{
    int x[]=new int[10];
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(" Enter 10 Integer values ");
    for(int i=0;i<10;i++)
    {
          x[i] = Integer.parseInt(br.readLine());      //input array of numbers
     }
     int h,p=0;
    for(int i=0;i<10;i++)
    {
        h=x[i]; 
        for(int j=i+1;j<10;j++)  //loop for comparison with next element
        {
            if(x[j]>h) //descending order
            {
                h=x[j];
                p=j;
            }
        }
        x[p]=x[i];  //swap
        x[i]=h;
    }
System.out.println(" After Sorting");
for(int i =0;i<=x.length-1;i++)
{
System.out.println(x[i]);
}
}
}