Java Blog

 
 
 
import java.io.*;
import java.util.*;   //for working with Vectors

class MergSort
{
         String str;
         int value=0;
         static int s1=0,s2=0;
         Vector list1 = new Vector();
         Vector list2 = new Vector();
         Integer intval; //object of Integer class for converting primitive numbers to object numbers

     void getdata()
     {
         try{
              System.out.println(\"\\n=====MERGE SORT=====\");
              BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
              System.out.println(\"\\nNOTE :  Data should be entered in sorted order\");
              System.out.print(\"\\nEnter size of First List : \");
              System.out.flush();
              str=obj.readLine();
              s1=Integer.parseInt(str);
                 for(int i=0;i<s1;i++)        
                    {
                      System.out.print(\"Enter value for Element \"+(i+1)+\" :  \");
                      System.out.flush();
                      str=obj.readLine();
                      value=Integer.parseInt(str);
                      intval = new Integer(value);
                      list1.addElement(intval);
                  }

                System.out.print(\"\\nEnter size of Second List : \");
              System.out.flush();
              str=obj.readLine();
              s2=Integer.parseInt(str);
                  for(    int i=0;i<s2;i++)
                   {
                      System.out.print(\"Enter value for Element \"+(i+1)+\" :  \");
                      System.out.flush();
                      str=obj.readLine();
                      value=Integer.parseInt(str);
                      intval=new Integer(value);
                      list2.addElement(intval);
                   }
            }
            catch(Exception e) {}
      }

                   //MERGE LOGIC
                   void merging()  //method to merg
                    {
                         for(int i=0;i<s2;i++)
                         {
                             list1.insertElementAt(list2.elementAt(i),(s1+i));
                        }
                        System.out.println(\"\\n\\nAFTER MERGING\");
                        for(int i=0;i<s1+s2;i++)
                        {
                            System.out.println(\"Element at(\"+(i+1)+\") : \"+list1.elementAt(i));
                        }
                   }

                //SORT LOGIC
                void sorting() //method for sorting 
                {
                    //creating temporary array
                    int size=list1.size();
                    Vector sort=new Vector();
                    int a,b; //extra for converting object to primitive numbers
                    Integer A,B; //extra for taking object value

                   int first=0,second=s1,third=s1+s2,i,j,c=0;
                   i=first;
                   j=second;
                   while(i<second && j<third)
                   {
                          A=(Integer)list1.elementAt(i);  //Assigning object to object
                          B=(Integer)list1.elementAt(j);  //Assigning object to object

                          a=A.intValue();  //converting object to primitive number
                          b=B.intValue();  //converting object to primitive number

                          if(a < b)   //list1.elementAt(i)   <    list1.elementAt(j)
                            {
                               sort.insertElementAt(A,c);
                               i++;
                            }
                          else
                            {
                              sort.insertElementAt(B,c);
                              j++;
                            }
                         c++;
                    }

                    if(i<second)
                    {
                          while(i<second)
                          {
                              sort.insertElementAt(list1.elementAt(i),c);
                              i++;
                              c++;
                         }
                    }

                    if(j<third)
                    {
                        while(j<third)
                        {
                              sort.insertElementAt(list1.elementAt(j),c);
                               j++;
                               c++;
                        }
                    }

                    System.out.println(\"\\n\\nAFTER SORTING\");
                    for(int k=0;k<sort.size();k++)
                          System.out.println(\"Element at(\"+(k+1)+\") : \"+sort.elementAt(k));
            }

};


class  sortdata
{
    public static void main(String[] args) 
    {
         MergSort obj=new MergSort();
         obj.getdata();
         obj.merging();
         obj.sorting();
    }
}

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

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

  3. An applet program that displays random dot

  4. Program to show the use of Break Statement

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

  6. Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server

  7. Corba program of date operations

  8. An applet of displaying simple moving banner

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

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

  11. Develop a RMI based application for addition of two complex numbers

  12. Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc

  13. Program to delete mth element from an input string

  14. Program to show the use of Conditional AND Operator (&&) in If Statements

  15. Program to find a factorial of a given number

  16. Program to show some properties of StringBuffer object

  17. Corba program of game

  18. Program to show the use of Nested Loops

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

  20. Program to show the use of Labeled Break Statement

 
 
Didn't find what you were looking for? Find more on Program to perform Merge Sort