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. Program to show an example of extracting subStrings from a String

  2. Program to show an example of using user-defined package in a program

  3. Program to display reverse $ tree

  4. Program of linear search

  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 declare, initialize and print an array of integers

  7. Program to show the use of Bitwise Operator (|) OR

  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. Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds()

  10. Program to calculate the frequency for different values of C starting from 0.01 to 0.1 in steps of 0.01

  11. Program to show an example of Concatination of Strings

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

  13. Program to read input from the Standard Input Device

  14. Program to show an example of extraction of characters from a StringBuffer objects

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

  16. Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side

  17. Program of insertion sort

  18. RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user

  19. Program that enabled applet calculator

  20. Program sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order

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