0

So I have some code that uses Merge Sort for an array int and I'm trying to format the code so it works with array Strings instead like having it sort by alphabetical order so ["peas", "zucchini", "apple", "berries"] turns into ["apple", "berries", "peas", "zucchini"]. If anyone could help me that would be great and much appreciated.

Here's my code

import java.util.Arrays;

public class MergeSortDemo
{
public static void main(String[] args)
 {
  int[] a = ArrayUtil.randomIntArray(20, 100);
  System.out.println(Arrays.toString(a));

  MergeSorter sorter = new MergeSorter(a);
  sorter.sort();
  System.out.println(Arrays.toString(a));
 }
}

The second part

import java.util.Random;

public class ArrayUtil
{

private static Random generator = new Random();

public static int[] randomIntArray(int length, int n)
{
int[] a = new int[length];

for (int i = 0; i < a.length; i++)
a[i] = generator.nextInt(n);

return a; 
 }
}

Final part

 public class MergeSorter
 {
  private int[] a;

  public MergeSorter(int[] anArray)
  {
  a = anArray;
  }


  public void sort()
  { 
  if (a.length <= 1) return;
  int[] first = new int[a.length / 2];
  int[] second = new int[a.length - first.length];

  for (int i = 0; i < first.length; i++) { first[i] = a[i]; }
  for (int i = 0; i < second.length; i++)
  {
     second[i] = a[first.length + i];
  }
  MergeSorter firstSorter = new MergeSorter(first);
  MergeSorter secondSorter = new MergeSorter(second);
  firstSorter.sort();
  secondSorter.sort();
  merge(first, second);
  }

private void merge(int[] first, int[] second)
 {  
  int iFirst = 0;
  int iSecond = 0;
  int j = 0;

  while (iFirst < first.length && iSecond < second.length)
  {
     if (first[iFirst] < second[iSecond])
     {
        a[j] = first[iFirst];
        iFirst++;
     }
     else
     {
        a[j] = second[iSecond];
        iSecond++;
     }
     j++;
  }
  while (iFirst < first.length)
  {
     a[j] = first[iFirst];
     iFirst++; j++;
  }

  while (iSecond < second.length)
  {
     a[j] = second[iSecond];
     iSecond++; j++;
   }
 }
}

2 Answers 2

1

Pretty much the only thing you have to change is the part in "part three" where you compare the two array-elements to see which one is bigger. Obviously you can't use a binary operator like '<' on a String, but the String class offers a compareTo(String s) method, which returns an int-value that is either positive or negative, depending on whether the compared String is lexicographically higher or lower, or zero if they are equal. For more detail look into the Java API.

Note: Obviously you need to change the int-Array to a String-Array to, but I think that goes without saying...

Sign up to request clarification or add additional context in comments.

3 Comments

Correct! Namely in the first if-clause in the merge method replace the < with compareTo(String 2) <= 0. That should work.
so all I have to change is if (first[iFirst] < second[iSecond]) to if (first[iFirst].compareTo(String 2) <= 0)? Where does String 2 come from
That’s the String you want to compare the first String with, in your case I’m assuming second[iSecond]. Everything else about that is documented in the API.
0

first you will remove ArrayUtil class and create and initialize your array in a MergeSortDemo class , and change the type of array into String , Here will be MergeSortDemo class.

import java.util.Arrays;

public class MergeSortDemo
{
  public static void main(String[] args) {
        // TODO code application logic here

  String [] a = {"peas", "zucchini", "apple", "berries"};
  System.out.println(Arrays.toString(a));

  MergeSorter sorter = new MergeSorter(a);
 sorter.sort();
 System.out.println(Arrays.toString(a));
    }

}

in final part you will change every int array to String array and your condition will be >> ( first[iFirst].compareTo(second[iSecond])<0 ),I see (<0) because compareTo method will return a value less than 0 if one String less than other .Here will be MergeSorter class.

class MergeSorter
 {
  private String[] a;

  public MergeSorter(String[] anArray)
  {
  a = anArray;
  }


  public void sort()
  { 
  if (a.length <= 1) return;
  String[] first = new String[a.length / 2];
  String[] second = new String[a.length - first.length];

  for (int i = 0; i < first.length; i++) { first[i] = a[i]; }
  for (int i = 0; i < second.length; i++)
  {
     second[i] = a[first.length + i];
  }
  MergeSorter firstSorter = new MergeSorter(first);
  MergeSorter secondSorter = new MergeSorter(second);
  firstSorter.sort();
  secondSorter.sort();
  merge(first, second);
  }

private void merge(String[] first, String[] second)
 {  
  int iFirst = 0;
  int iSecond = 0;
  int j = 0;

  while (iFirst < first.length && iSecond < second.length)
  {
     if (first[iFirst].compareTo(second[iSecond])<0)
     {
        a[j] = first[iFirst];
        iFirst++;
     }
     else
     {
        a[j] = second[iSecond];
        iSecond++;
     }
     j++;
  }
  while (iFirst < first.length)
  {
     a[j] = first[iFirst];
     iFirst++; j++;
  }

  while (iSecond < second.length)
  {
     a[j] = second[iSecond];
     iSecond++; j++;
   }
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.