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  InsSort()
      {
            System.out.println(\"=====INSERTION SORT=====\\n\");
            getdata();
            int i,j=0,temp=0;
                for(i=0;i<size;i++)
                    {
                     if(sortArr[i]<sortArr[i-1])
                     {
                        temp=sortArr[i];
                       for(j=i-1;j>=0;j--)
                        {
                           sortArr[j+1]=sortArr[j];
                           if(sortArr[j-1]<=temp || j==0)
                              break;
                        }
                     }
                    sortArr[j]=temp;
                  }
            display();
        }    

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


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

    Related Post:
  1. Corba program to Write a Echo server and client with UDP server and client

  2. An applet program to display moving banner

  3. Write a class whose objects holds a current value and have a method to add that value, printing the new value

  4. Program to show an example of using initialization block to initialize the data members of a class

  5. Program that will read real number from the keyboard and print the following output in one line: small Integer not less The Given Number Largest Int

  6. Program to show the use of some Character Methods

  7. Program using Switch case to compute the net amount to be paid by a customer

  8. Game application in CORBA for which player will guess a number between 1 to 100, which will be compared to the random number generated by the class

  9. Program of Floyds Triangle

  10. Program to show the use of operator (&lt;&lt;) Shift Left filling with zero from the right

  11. Program of inventory management and demonstrating interactive input and output

  12. Program of bubble sort

  13. Pass the marks of 10 students from the client side design a servlet, which takes these marks and returns the number of students who obtained first cls

  14. Develop a game application in CORBA for which the player will guess a number between 1 to 100, which will be compared to the random number generated b

  15. Program to show the use of Logical OR Operator (|) in If Statements

  16. Simple Applet which will show life cycle of an Applet

  17. Servlet which takes input from an HTML client page and calculates and outputs the premium amount

  18. Program to show an example of Driving and using a Class

  19. An applet program that displays random dot

  20. Program to show the use of Logical or Boolean Negation i.e. NOT (!)

 
 
Didn't find what you were looking for? Find more on Program of insertion sort