Wednesday, 2 October 2013

mergesorting array not working with large arrays

mergesorting array not working with large arrays

could someone tell me why my code can't sort arrays of large sizes,
basically anything larger than size 3. I've been debugging for a while but
can't seem to find the problem, any help is appreciated. Thanks alot.
import java.util.*;
import java.io.*;
// No need to change anything in this class
class MergeSortQuestion {
// merges sorted subarrays A[start...firstThird],
A[firstThird+1,secondThird], and A[secondThird+1,stop]
public static void mergeThreeWay(int A[], int start, int firstThird,
int secondThird, int stop)
{
int indexLeft = start; //index for first third half
of array A
int indexMiddle = firstThird+1; //index for the middle element
of A
int indexRight = secondThird+1; //index for the right half of A
int [] temp = new int [A.length]; //create a new temporary array
of same type and size of array A
int tempIndex = start; //index for the temporary
array
if (tempIndex <= stop) {
//The following is valid if all 3 subdivisions of the array are
not used up
while ((indexLeft <= firstThird) && (indexMiddle <=
secondThird) && (indexRight <=stop))
{
if ((A[indexLeft] <= A[indexMiddle]) && (A[indexLeft] <=
A[indexRight]))
{
temp[tempIndex] = A[indexLeft]; //select the left element
indexLeft++;
}
else if ((A[indexMiddle] <= A[indexLeft]) &&
(A[indexMiddle] <= A[indexRight]))
{
temp[tempIndex] = A[indexMiddle]; //select the middle
element
indexMiddle++;
}
else if ((A[indexRight] <= A[indexLeft]) && (A[indexRight]
<= A[indexMiddle]))
{
temp[tempIndex] = A[indexRight]; //select the right
element
indexRight++;
}
tempIndex++;
}
//Executes when the last 3rd of the array has been used up
while ((indexLeft <= firstThird) && (indexMiddle
<=secondThird) && (indexRight > stop))
{
if (A[indexLeft]<= A[indexMiddle])
{
temp[tempIndex] = A[indexLeft];
indexLeft++;
}
else if (A[indexLeft] >= A[indexMiddle])
{
temp[tempIndex] = A[indexMiddle];
indexMiddle++;
}
tempIndex++;
}
//Executes when the middle half of the array has been used up
while ((indexLeft <= firstThird) && (indexMiddle >
secondThird) && (indexRight <= stop))
{
if (A[indexLeft]<= A[indexRight])
{
temp[tempIndex] = A[indexLeft];
indexLeft++;
}
else if (A[indexLeft] >= A[indexRight])
{
temp[tempIndex] = A[indexRight];
indexRight++;
}
tempIndex++;
}
//Executes when the first half of the array has been used up
while ((indexLeft > firstThird) && (indexMiddle <=
secondThird) && (indexRight <= stop))
{
if (A[indexMiddle] <= A[indexRight])
{
temp[tempIndex] = A[indexMiddle];
indexMiddle++;
}
else if (A[indexMiddle] >= A[indexRight])
{
temp[tempIndex] = A[indexRight];
indexRight++;
}
tempIndex++;
}
//Executes in the case where both the left and middle sections
are used up
while ((indexLeft > firstThird) && (indexMiddle > secondThird)
&& (indexRight <= stop))
{
temp[tempIndex]=A[indexRight];
indexRight++;
tempIndex++;
}
//Executes in the case where both the left and last sections
are used up
while ((indexLeft > firstThird) && (indexMiddle <=
secondThird) && (indexRight > stop))
{
temp[tempIndex]=A[indexMiddle];
indexMiddle++;
tempIndex++;
}
//Executes in the case where both the middle and last sections
are used up
while ((indexLeft <= firstThird) && (indexMiddle >
secondThird) && (indexRight > stop))
{
temp[tempIndex]=A[indexLeft];
indexLeft++;
tempIndex++;
}
}
//Now, we copy the temporary array back into array A
for (int i=0; i<A.length; i++) {
A[i]=temp[i];
}
}
// sorts A[start...stop]
public static void mergeSortThreeWay(int A[], int start, int stop) {
if (((stop-start)==1)&&(A[1]<A[0])){
int temp = A[1];
A[1] = A[0];
A[0] = temp;
}
if (start < stop) {
int firstThird = ((stop-start)/3 + start);
int secondThird = 2*(stop-start)/3 + start;
mergeSortThreeWay (A, start, firstThird);
mergeSortThreeWay (A, firstThird+1,secondThird);
mergeSortThreeWay (A, secondThird+1, stop);
mergeThreeWay(A, start, firstThird, secondThird, stop);
}
}
public static void main (String args[]) throws Exception {
int myArray[] = {3,1,4}; // an example array to be sorted. You'll need
to test your code with many cases, to be sure it works.
mergeSortThreeWay(myArray,0,myArray.length-1);
System.out.println("Sorted array is:\n");
for (int i=0;i<myArray.length;i++) {
System.out.println(myArray[i]+" ");
}
}
}

No comments:

Post a Comment