Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: MERGE SORT pls help

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MERGE SORT pls help

    this is correct already i just nid to display the part by part of merge sort steps

     
    public class mergesortinteger {
     
     
     
      public static int a[]=new int[50];
     
      public static void merge_sort(int low,int high)
    {
     int mid;
     if(low<high)
     {
      mid=(low+high)/2;
      merge_sort(low,mid);
      merge_sort(mid+1,high);
      merge(low,mid,high);
     }
    }
      public static void merge(int low,int mid,int high)
    {
     
     int h,i,j,k;
     int b[]=new int[50];
     h=low;
     i=low;
     j=mid+1;
     while((h<=mid)&&(j<=high))
     {
      if(a[h]<=a[j])
      {
       b[i]=a[h];
       h++;
      }
      else
      {
       b[i]=a[j];
       j++;
      }
      i++;
     }
     if(h>mid)
     {
      for(k=j;k<=high;k++)
      {
       b[i]=a[k];
       i++;
      }
     }
     else
     {
      for(k=h;k<=mid;k++)
      {
       b[i]=a[k];
       i++;
      }
     }
     for(k=low;k<=high;k++) a[k]=b[k];
    }
     
     
        public static void merge_integer(){
     
        int num,i;
     
     System.out.println("N series");
     num=new Scanner(System.in).nextInt();
     System.out.println();
     System.out.println("Now, Please Enter ("+ num +") nos.:");
     for(i=1;i<=num;i++)
     {
      a[i]=new Scanner(System.in).nextInt() ;
     }
     
     merge_sort(1,num);
     System.out.println();
     System.out.println("Merge Sorted[Integer]:");
     for(i=1;i<=num;i++)
         System.out.println(a[i]+"    ");
     
     
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: MERGE SORT pls help

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MERGE SORT pls help

    edited .... how will i display the sorting .....

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: MERGE SORT pls help

    how will i display the sorting
    Can you post an example of the output you want from the program?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MERGE SORT pls help

    example :
    N series: 8
    Input Value:
    5
    7
    4
    3
    2
    8
    6
    1
     
    DISPLAY :   
                 5 7 4 3 2 8 6 1
                5 7 4 3   2 8 6 1
               5 7   4 3   2 8   6 1
             5  7  4  3    2  8  6  1 
             5 7   3 4      2 8   1 6
              3 4 5 7        1 2 6 8
                 1 2 3 4 5 6 7 8 <-----FINAL answer


    --- Update ---

    the spaces in the display doesnt matter

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: MERGE SORT pls help

    Is the print out the contents of the arrays?
    To print the contents of an array on a line, use a loop and the print() statement.
    Call the println() method or add a "\n" at the end of a line when you want the next output to go on the next line.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MERGE SORT pls help

    my program only display the sorted array ...not displaying the step by step sorting of a merge sort

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: MERGE SORT pls help

    You need to add code as discussed in post #6
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. K-way merge sort problem
    By yotabytes in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 7th, 2013, 08:12 PM
  2. Sort and Merge Two Linked List
    By Loraeron in forum What's Wrong With My Code?
    Replies: 49
    Last Post: February 28th, 2013, 09:08 PM
  3. Merge Sort
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2013, 09:00 AM
  4. heap sort vs merge sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 3rd, 2012, 03:37 AM
  5. [SOLVED] displaying every 1000 comparisons in merge sort
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 27th, 2012, 07:26 PM