Problem with Merge Sort Alg Implementation

  • Java
  • Thread starter zak100
  • Start date
  • Tags
    Sort
In summary, the conversation is about implementing merge sort algorithm in Java and the issue of not getting a sorted list. The code provided includes a class called MergeAlg2 that has a merge method and a merge_sort method. The main method calls the merge_sort method to sort the initial list {2, 4, 5, 7, 1, 3, 6, 8}. The issue is that after sorting, the list becomes 2, 3, 4, 5, 6, 7, 1, 8. The conversation also suggests using code tags for readability and debugging the code using a debugger or print statements. It is also recommended to add comments to make the code more intuitive and
  • #1
zak100
462
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
  • #2
It would be helpful if you would use the code tags so your code would be readable.
 
  • #3
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.
 
  • #4
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)
 

What is merge sort algorithm?

Merge sort is an efficient sorting algorithm that works by dividing the unsorted list into smaller sublists, sorting those sublists, and then merging them back together to get the final sorted list.

What is the problem with merge sort algorithm implementation?

The main problem with merge sort algorithm implementation is that it requires additional space and memory to store the sublists during the sorting process. This can be an issue for large datasets as it can slow down the algorithm and make it less efficient.

How can the problem with merge sort algorithm implementation be solved?

The problem with merge sort algorithm implementation can be solved by implementing it in a way that reduces the need for additional space and memory. This can be done by using an iterative approach instead of a recursive one, or by using a bottom-up merge sort method.

What is the time complexity of merge sort algorithm?

The time complexity of merge sort algorithm is O(n log n), where n is the number of elements in the list. This makes it one of the most efficient sorting algorithms, especially for large datasets.

Can merge sort algorithm handle duplicates and negative numbers?

Yes, merge sort algorithm can handle duplicates and negative numbers. It is a stable sorting algorithm, meaning that the relative order of equal elements is preserved during the sorting process. It can also handle negative numbers as it compares elements based on their values, rather than their indices.

Similar threads

  • Programming and Computer Science
Replies
1
Views
880
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
Back
Top