Go Back   Java Programming Forums > Java Standard Edition Programming Help > Algorithms & Recursion


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 17-02-2010, 12:06 AM
Junior Member
 

Join Date: Oct 2009
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
mgutierrez19 is on a distinguished road
Default MergeSort

I am trying to implement recursive mergesort algorithm and it is almost done the only thing is that if i want to mergesort ( 7, 1 ,2, 3, 5, 6, 8, 4) when i run the program my output is (0, 0, 1, 2, 3, 4, 5, 6, 7, 8)

What is wrong with my code???

Java Code
import java.util.*;
import java.io.*;

public class MergeSort
{

public static void mergeSort(double[] a, int length)
   {  
     Sort(a, 0, a.length - 1);
   }
public static void Sort(double[] a, int left, int right)
   {  
     if (left < right)
     {
      int mid = (left + right) / 2;
       // sort the first and the second half
      Sort(a, left, mid);
      Sort(a, mid + 1, right);
      merge(a, left, mid, right);
     }
   }
   public static void merge(double[] a, int left, int mid, int right)
   {  

      double[] b = new double[right - left + 1]; // merge both halves into a temporary array b

      int index1 = left;         // next element to consider in the first range
      int index2 = mid + 1;
      int last = right;
      int index = 0;             // next open position in b

      // as long as neither i1 nor i2 past the end, move the smaller into b
      while(index1 <= mid && index2 <= last)
      {  
        if(a[index1] < a[index2])
         {  
          b[index] = a[index1];
            index1++;
         }
         else
         { 
           b[index] = a[index2];
            index2++;
         }
         index++;
      }

      // note that only one of the two while loops
      // below is executed

      // copy any remaining entries of the first half
      while (index1 <= mid)
      { 
        b[index] = a[index1];
         index1++;
         index++;
      }

      // copy any remaining entries of the second half
      while (index2 <= last)
      {  
        b[index] = a[index2];
         index2++;
         index++;
      }
      int s = 0;
      // copy back from the temporary array
      for(int i = left; i <= right; i++)
      {
         a[i] = b[s];
         s++;
      }
   }

public static void main(String[] args)throws FileNotFoundException
{
  Scanner input = new Scanner(new FileReader("input.txt"));
  
  int count = 0;
  int numOfElements = 0;  
  double[] array = new double[10];


  while(input.hasNextDouble())
  {
    array[count] = input.nextDouble();
    count++;
  }
        System.out.println("Unsorted Array");
    for(int i = 0; i < count; i++)
    {
       System.out.print(array[i] + " ");
  }
    System.out.println();
    mergeSort(array, array.length);
    System.out.println("Sorted Array");
    for(int i = 0; i < count; i++)
    {
      System.out.print(array[i] + " ");
    }

input.close();
 }
}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 17-02-2010, 12:13 AM
Junior Member
 

Join Date: Oct 2009
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
mgutierrez19 is on a distinguished road
Default Re: MergeSort

correction my output when i run the program is (0,0,1,2,3,4,5,6) so it leaves out that last two integers of my array
Reply With Quote
  #3 (permalink)  
Old 17-02-2010, 02:22 AM
Member
 

Join Date: Feb 2010
Location: Auburn, AL
Posts: 31
Thanks: 0
Thanked 8 Times in 8 Posts
CT&SC is on a distinguished road
Default Re: MergeSort

I don't see any problem with your code right off. When I run your code and manually set the array to be random-ish values, it works. Is it possible you aren't reading what you think you're reading from the file?
__________________
Let me know what you think of my website:
http://www.auburn.edu/~carpept
Comments or suggestions are appreciated!
Reply With Quote
  #4 (permalink)  
Old 04-03-2010, 12:46 AM
Junior Member
 

Join Date: Feb 2010
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
Shadow703793 is on a distinguished road

I'm feeling Cheerful
Default Re: MergeSort

Hey m8, you may find the Mergesort I have here: Quick Question about Mergesort useful as a reference. It works for the numbers I have tried.

You may also find this psudocode useful:
Java Code
function mergesort(m)
   var list left, right, result
   if length(m) ≤ 1
       return m
   else
       var middle = length(m) / 2
       for each x in m up to middle - 1
           add x to left
       for each x in m at and after middle
           add x to right
       left = mergesort(left)
       right = mergesort(right)
       if last(left) ≤ first(right) 
          append right to left
          return left
       result = merge(left, right)
       return result

function merge(left,right)
   var list result
   while length(left) > 0 and length(right) > 0
       if first(left) ≤ first(right)
           append first(left) to result
           left = rest(left)
       else
           append first(right) to result
           right = rest(right)
   if length(left) > 0 
       append rest(left) to result
   if length(right) > 0 
       append rest(right) to result
   return result
Source: http://rosettacode.org/wiki/Sorting_...hms/Merge_sort

Last edited by Shadow703793; 04-03-2010 at 12:52 AM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:58 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.