Java Tutorial

 
 
 
 
import java.io.*;

class sort
{
      String str;
      int size,sortArr[];

      public void getdata()
      {
             System.out.print(\"Enter how many data you want to enter : \");
             System.out.flush();
             try{
                 BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
                 str=obj.readLine();
                 size=Integer.parseInt(str);
                 sortArr=new int[size];
                 for(int i=0;i<size;i++)
                   {
                       System.out.print(\"Enter element at \"+(i+1)+\"th 

position  :  \");
                       System.out.flush();
                       str=obj.readLine();
                       sortArr[i]=Integer.parseInt(str);
                   }
                }
            catch(Exception e)  {}
      }

      public void  SelSort()
      {
            System.out.println(\"=====SELECTION SORT=====\\n\");
            getdata();
            int min;
                for(int i=0;i<size;i++)
                    {
                        min=i;
                  for(int j=i+1;j<size;j++)
                      {   
                          if(min>=size)
                                  min=size;
                          if(sortArr[min]>sortArr[j])
                                    min=j;
                      }
                   int temp=sortArr[i];
                   sortArr[i]=sortArr[min];
                   sortArr[min]=temp;
                  }
            display();
        }    

    public void display()
    {
         System.out.println(\"\\nAfter Sorting\");
         for(int i=0;i<size;i++)
              System.out.println(sortArr[i]);
    }
}


class SelSort 
{
    public static void main(String args[]) 
    {
          sort ob1=new sort();
          ob1.SelSort();
    }
}

 
 
Didn't find what you were looking for? Find more on An applet program to perform selection sort