[java] help

tmc

Veteran X
yeah...another homework help thread...

Basically, I need to sort an array in java using recursion, and a method which merges 2 arrays that are already sorted.
ie, start with an array, break it up into smaller arrays recursively, then use the other method to sort these smaller arrays and return the sorted large array.

The method that merges 2 arrays:
Code:
public static int[] mergeSort(int[] anArray, int left, int right)
  {int largest;
    int index;
    int temp;

    if (right!=left) //If right is equal to left, then the array will be sorted, and the program can end and return the value.  If not, it will do recursion to sort the remainder.
    {
      //Find largest value
      for (index=0,largest=0;index<=right;index++)
      {
        if (anArray[index]>anArray[largest])
        {
          largest = index;
        }
        else
        {
          ; //nothing
        }
      }
      
      //Swap the largest value with the last digit
      temp = anArray[right];
      anArray[right] = anArray[largest];
      anArray[largest]=temp;
      
      //Recursion
      mergeSort(anArray,left,right-1);
    }
    else
    {
      ; //nothing
    }
    return anArray;
  }

the header of the new method I must create to sort a big array:
Code:
public static int[] mergeSort(int[] anArray, int left, int right)

I dont necessarily need a working code, but I cant figure out how to do this...

thx to whoever can help
 
Back
Top