Problem with Merge Sort Alg Implementation

  • Context: Java 
  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Sort
Click For Summary

Discussion Overview

The discussion revolves around a participant's implementation of the merge sort algorithm in Java, focusing on issues related to incorrect output after sorting. Participants are providing guidance and suggestions for debugging the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the output of the sorting algorithm is incorrect and provides the initial and resulting lists for comparison.
  • Another participant suggests using code tags for better readability of the posted code.
  • It is proposed that the original poster should step through the code using a debugger or print statements to identify where the sorting decisions are made.
  • A participant emphasizes the need to clarify the meaning of the parameters p, q, r in the merge function and suggests adding comments to improve code understanding.
  • There is a recommendation to change the initial call to the merge sort function to use indices that start from 0, which may align better with conventional array indexing.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the specific issues within the code, and multiple suggestions for debugging and improving the implementation are presented without agreement on a single solution.

Contextual Notes

There are unresolved aspects regarding the interpretation of the parameters used in the merge function and the indexing of the array, which may affect the sorting logic.

zak100
Messages
462
Reaction score
11
Hi,
I am trying to implement merge Sort Alg in Java. I am not getting any error but its not printing sorted list . can some body please guide me? My code is:
Java:
import javax.swing.*;

public class MergeAlg2{
   static int[ ] arr={2, 4, 5, 7, 1, 3, 6, 8}; 
 
   int [ ] L;
   int [ ] R;
   void merge(int [ ] arr, int p, int q, int r){
      int n1 = q- p +1;
      int n2 = r-q;
      L = new int[n1+1];
      R = new int[n2+1];
      int i=0;
      int j=0;
      for(  i=0;  i< n1; ++i)
         L[I] = arr[p +i -1];
      for(j=0; j<n2; ++j)
         R[j] = arr[q+j];
      L[n1] =100000;
      R[n2]= 100000;
      i=0;
      j=0;
      int k=0;
      for(k=p-1; k<r; ++k){
         if(L[ i ] <= R[j]){
            arr[k] = L[ i ];
           i++;
         }
         else{
            arr[k] = R[j];
            j++;
         }
      }
   }
   void merge_sort(int[ ] a, int left, int right){
      int i=0;
      String str="";
      if(left <right) {
         JOptionPane.showMessageDialog(null," Enters left = " + left  + "right= " + right );
         int center = (left + right)/2;
         merge_sort(a, left, center);
         merge_sort(a, center + 1, right);
         merge(a, left, center, right);
       }
       for(i=0; i<=7; ++i){
         str= str + a[ i ];
      } 
      JOptionPane.showMessageDialog(null, "Sorted List is" + str);
   }
   public static void main(String[ ] args){
      MergeAlg2 obj = new MergeAlg2();
      obj.merge_sort(arr, 1,8);
      int i=0;
   
      String str="";
      for(i=0; i<=7; ++i){
         str= str + arr[ i ];
      } 
      JOptionPane.showMessageDialog(null, str);
  }
}

My initial list is: {2, 4, 5, 7, 1, 3, 6, 8};

But after sorting i am getting: 2, 3, 4, 5, 6, 7, 1, 8

Zulfi.
 
Last edited by a moderator:
Technology news on Phys.org
It would be helpful if you would use the code tags so your code would be readable.
 
Have you tried stepping through your code via a debugger or using print statements?

Look at where the sort makes a decision and decide if that's what it should do.
 
The first thing you need to do is decide what the p,q,r parameters mean and then follow that decision throughout your code.
Since your use is somewhat unintuitive, you may need to add comments such as
Java:
//first chunk:  a[p-1] to a[q-1]
//second chunk: a[q] to a[r]

You may want to change the arguments p,q,r so that the first call would become
Java:
mergesort(arr, 0, 8)
and the split would be
Java:
mergesort(a, left, center)
mergesort(a, center, right)
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 97 ·
4
Replies
97
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K