amartin94
amartin94

Reputation: 515

How to MergeSort a String ArrayList

so i've been trying to figure out this code for the past couple of days now and im getting nowhere. This is the furthest i've got, but now it's not displaying the final sorted array, and any code i try and add past the return merged statement is apparently unereachable :/

import java.util.*;

public class MergeTestStringArray
{


  public static void main(String[] args)
  {
    ArrayList< String > array = new ArrayList< String >();

    array.add("John");
    array.add("Victor");
    array.add("Joe");
    array.add("Jackson");
    array.add("Anthony");
    array.add("Angelina");
    array.add("George");
    array.add("Paul");

    ArrayList< String > sortedArray = new ArrayList< String >();

    sortedArray = mergeSort(array);

    for (int i = 0; i < sortedArray.size(); i++)
    {
        System.out.println(" " + sortedArray.get(i) );
    }
  }

  public static ArrayList< String > mergeSort(ArrayList< String > list)
  {
    ArrayList < String > sorted = new ArrayList< String >();
    if (list.size() == 1)
    {
        sorted = list;
    } else {
        int mid1 = list.size() /2;

        ArrayList< String > left = new ArrayList< String >();
        ArrayList< String > right = new ArrayList< String >();

        for ( int x = 0; x < mid1; x++) {
            left.add(list.get(x));

        }
        for ( int x = mid1; x < list.size(); x++) {
            right.add(list.get(x));
        }

        System.out.println("Left Array: " + left);
        System.out.println("Right Array)" + right);

        mergeSort(left);
        mergeSort(right);
        mergeArray(left,right);
    }

    return sorted;
  }

private static ArrayList< String > mergeArray(ArrayList< String > left, ArrayList< String > right)
{
    ArrayList< String > merged = new ArrayList< String >();

    int i = 0;
    int l = 0;
    int r = 0;

    while (l < left.size() && r < right.size())
           {
              if ((left.get(l)).compareTo(right.get(r)) < 0)
              {
                 merged.add(left.get(l));
                 l++;
              }
              else
              {
                 merged.add(right.get(r));
                 r++;
              }

              i++;
           }


           while (l < left.size())
           {
              merged.add(left.get(l));
              l++;
              i++;
           }

           // Append rest of the values in the right half, if any...
           while (r < right.size())
           {
              merged.add(right.get(r));
              r++;
                  i++;
           }

    return merged;


  }

}

Upvotes: 1

Views: 4823

Answers (3)

IceMan
IceMan

Reputation: 1426

assign the return value of mergeArray to sorted.

sorted = mergeArray(left,right);

And assign the results of your mergeSort calls:

        left = mergeSort(left);
        right = mergeSort(right);

Also, you do not need to create an empty ArrayList to declare a variable, when you are not going to use it.

The following will suffice:

ArrayList < String > sorted;

Upvotes: 0

Benjamin Barenblat
Benjamin Barenblat

Reputation: 1311

Your error is in your mergeSort function. Specifically, in your recursive case, you never actually save the result of the sort to the merged list. Try replacing

mergeArray(left,right);

with

sorted = mergeArray(left,right);

Upvotes: 1

Doboy
Doboy

Reputation: 11082

Not sure what your question is but.. statements after return statements are generally unreachable. Once the function reaches the return statement it will exist the function and return the value at that return statement..

Upvotes: 1

Related Questions