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. Program to display following things on the client machine a)date and time of the server machine b)date and time of the client machine and difference

  3. File handling program to copy characters from one file to another

  4. Program to show an example of using a Static Nested Class

  5. Program to display triangle of numbers

  6. Encrypt the text that you want to send on the client side. The server side decrypts it after receiving. Append server time after each word in the sent

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

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

  9. Program to show an example of modifying String objects

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

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

  12. Even number ,Prime no. ,Factorial of a number

  13. RMI based application for login validation based on data saved in text file stored on the server and will display appropriate message to the client..

  14. Program of FTP client and server

  15. An applet program to perform quick sort

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

  17. Program to read the price of an item in decimal form (like 75.95) and print the output in paise (like 7595 paise).

  18. Program to generate Fibonacci Series of a given number

  19. Program to declare, initialize and print a StringBuffer object

  20. Program of Floyds Triangle

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