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 append a StringBuffer by StringBuffer, String objects and other Basic Data Types

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

  3. Program of telnet server - client

  4. Program of an applet to display alternate filled and empty circle

  5. Program of FTP client and server

  6. Program to show an example of using user-defined package in a program

  7. Program to declare, initialize and print a String object

  8. An applet program to draw different shapes

  9. Program to show the use of Switch Statement

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

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

  12. Program to show the comparing of Strings for equality

  13. An applet program of showing new document using showDocument(URL) method

  14. Program for Thread Priority

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

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

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

  18. GUI application in java, which enter the details of a student and on the submit display the details of the student

  19. An applet program to display moving banner

  20. Program to sort the contents of an array using Bubble Sort

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