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. Application for counting factorial of a given number

  2. Program to show an example of Calling a Base Class Method from a Derived Class

  3. Program to check the start and end of a String

  4. Program to show the use of Increment and Decrement Operators

  5. Program to read bytes from a file

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

  7. An applet program to display alternate filled and empty circle that to in row and column format

  8. Program that enabled applet that takes name of the user. Server returns back the number in uppercase clubbed with “HELLO” and display it on applet

  9. Corba program of UDP client server application which sends the news to the client. Server takes the news from the NewsDataFile located at the server

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

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

  12. Program to display reverse $ tree

  13. Socket program of multi client chat server

  14. Program in CORBA which takes a long sentence from client (at least 7 words), passes to the server, server separates out each word and sends back

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

  16. An applet program to display moving banner

  17. An applet program to display text

  18. File Handling- JAVA

  19. Program to read command line arguments

  20. Word list program that reads a text file and makes an alphabetical list of all the words in that file. The list of words is output to another file

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