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. 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

  2. An applet program to draw concentric circle in center of the canvas

  3. Program to convert the given temperature in faherenhiet to celsius using the following conversion formula C= F- 32/1.8

  4. An applet program that creates string dynamically and displays it

  5. Develop a CORBA application that takes a string from client and returns whether it is palindrome

  6. Program to declare and initialize Integer variables and print them on the Standard Output Device

  7. An applet program to display barchart of given data

  8. Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc

  9. Program to show the use of operator (&gt;&gt;) Shift Right, propagating the sign bit from the left

  10. Program to show the use of Bitwise Operator (^) XOR (Exclusive OR)

  11. Program to copy the elements of an array into another array in reverse order and print them

  12. Program to show some properties of StringBuffer object

  13. Program to change characters in a StringBuffer objects and to reverse its contents

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

  15. Program to read the table elements into a two-dimensional array temperature

  16. Program of telnet server - client

  17. Program to read command line arguments

  18. Program to evaluate following investment equation V=P(1+R)^N

  19. An applet program to display text

  20. Program to show the use of Nested If-Else Statements

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