merge sort

i keep getting out of bounds i don't know why

Page 1 of 1

1 Replies - 1027 Views - Last Post: 25 February 2010 - 07:06 AM Rate Topic: -----

#1 chaconmari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-February 10

merge sort

Posted 24 February 2010 - 11:23 PM

can someone help me Im not sure if the algorithm is right but i keep getting an array out of bounds exception its suppose to do a merge sort without recursion


import java.util.Scanner;
import java.util.Stack;
public class part2
{
//object for stack info
public static class MergeSortObject
{
public boolean merge;
public int first;
public int last;

//constructor
public MergeSortObject(boolean mergeIn, int firstIn, int lastIn)
{
merge = mergeIn;
first = firstIn;
last = lastIn;
}
}

//this method takes the given array and merges the two parts together
private static void merge(int [ ] data, int first, int n1, int n2 )
{
int []tmpArray = new int[n1+n2];
int copied = 0;
int copied1 = 0;
int copied2 = 0;

// Main loop
while((copied1 < n1) && (copied2 < n2) )
{
if(data[first+ copied1] < data[first+n1+copied2] )
{
tmpArray[copied++] = data[first + (copied1++)];
}
else
{
tmpArray[copied++] = data[first + n1 + (copied2++)];
}
}
// Copy rest of first half
while(copied1 < n1 )
{
tmpArray[copied++] = data[first + (copied1++)];
}
// Copy rest of right half
while(copied2 < n2)
{
tmpArray[copied++] = data[first + n1 + (copied2++)];
}
// Copy tmpArray back
for( int i = 0; i < n1+n2; i++)
{
data[first+i] = tmpArray[i];
System.out.print(data[first+i]);

}

}

//this method goes through the
public static void StackMergeSort(int [] A)
{
MergeSortObject store = new MergeSortObject(false, 0, A.length);
Stack<MergeSortObject> mStack = new Stack<MergeSortObject>();
mStack.push(store);

while(!mStack.isEmpty())
{
store = mStack.pop();
//calls the merge method
if(store.merge)
{
merge(A, store.first, (store.first + (store.last - store.first)/2), store.last);
}
else if(store.first < store.last)
{
//stores values in stack
MergeSortObject one = new MergeSortObject(true, store.first, store.last);
MergeSortObject two = new MergeSortObject(false, store.first, (store.first+store.last)/2);
MergeSortObject three = new MergeSortObject(false, (store.first+store.last)/2+1, store.last);
mStack.push(one);
mStack.push(two);
mStack.push(three);
}
}
}


public static void main(String[]args)
{
System.out.println("Please give the size of the array you would like to sort:");
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int [] A = new int[size];
for(int i = 0; i<size; i++)
{
if(i == 0)
{
System.out.println("Input the first variable:");
A[i] = scan.nextInt();
}
else if(i == size -1)
{
System.out.println("Input the last variable:");
A[i] = scan.nextInt();
}
else
{
System.out.println("Input the next variable:");
A[i]= scan.nextInt();
}
}
StackMergeSort(A);

System.out.print("Unsorted Array: ");
for(int i = 0; i<A.length; i++)
{
System.out.print(A[i] + " ");
}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: merge sort

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: merge sort

Posted 25 February 2010 - 07:06 AM

could you please post the error messege you get? what line? what Object throws it..
also, please post your code inside code tags.
Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1