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 that will count the number of lines in each file that is specified on the command line

  2. Program to extract a portion of a character string and print the extracted string

  3. Program to show an example of searching Strings for subStrings

  4. Program to show the use of Switch Statement for the same statement for several different case labels

  5. Program to print triangle of numbers

  6. Program to determine the sum of the following harmonic series for a given value of n

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

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

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

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

  11. Program to show an example of using constructors in a class to initialize its data members

  12. Program to print a string on Standard Output Device

  13. Program to make directory

  14. An applet program to draw Polygon Graph with given data

  15. Program to ask question and accept answer up to 3 attempt and display right answer if it is wrong

  16. Corba program to encrypt the text that you want to send on the client side. The server side decrypts it after receiving. Append server time.

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

  18. Biological Sequence Allignment Using Dynamic Programing

  19. Program to find a factorial of a given number

  20. Program to show the use of operator (&gt;&gt;&gt;) Shift Right, filling with zeros from the left

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