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 creating a String object from a StringBuffer Object

  2. Do the following for the Array Insert the element in the array,delete an element from the array,search for a particular element Create multiple thread

  3. Corba program of game

  4. Given a list of marks ranging form 0 to 100, write a program to compute and print the number of students marks

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

  6. Program to show the use of Logical AND Operator (&) in If Statements

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

  8. Application which takes your marks of six subjects from the client side and displays the percentage that you have obtained

  9. An applet program to display sum of two number of textField

  10. Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

  11. Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds()

  12. Program to show the use of Do-While Loop

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

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

  15. Program to show an example of overloading constructors in a class to initialize its data members

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

  17. Program to generate SQUARE ROOT table

  18. Program to show an example of using a Non-Static Nested Class

  19. An applet program that displays Text at the center of the string which is passed as parameter

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

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