Reputation: 29
How would you write a method that takes 2 string arrays and merges them into one array in order by the length of each element in the array by using mergeSort. And by using compareToIgnoreCase?
This is the code that I have so far. I dont even know if im doing it right so far; I'm just trying to make my merge sort work.
public static String[] merge(String[] arr1, String[] arr2) {
String index1 = "";
String index2 = "";
String[] result = new String[arr1.length + arr2.length];
while (index1 < arr1.length && index2 < arr2.length) {
if (arr1[index1] < arr2[index2]) {
result[index1 + index2] = arr1[index1];
index1++;
} else {
result[index1 + index2] = arr2[index2];
index2++;
}
}
if (index1 == arr1.length){
for (String i = index2; i < arr2.length; i++) {
result[index1 + i] = arr2[i];
}
} else if (index2 == arr2.length){
for (String i = index1; i < arr1.length; i++) {
result[index2 + i] = arr1[i];
}
}
return result;
}
Upvotes: 0
Views: 887
Reputation: 6890
You can use following step:
ArrayUtile
in Apache Commons Lang3.Comparatore
interface for implementing ignore case.Arrays
utilitin java.util
.I write this code as utility method:
public void sort(String[] one, String[] two)
{
String[] merged_array = ArrayUtils.addAll(one, two);
Comparator<String> ignorCase_comparatore = new Comparator<String>()
{
@Override
public int compare(String o1,
String o2)
{
return o1.compareToIgnoreCase(o2);
}
};
Arrays.sort(merged_array, ignorCase_comparatore);
}
I hope my answer useful for you.
Upvotes: 0
Reputation: 3332
Here's a really literal code snippet that answers your question. It uses a merge sort algorithm to combine two string arrays (must merge sort each individually first) into one sorted array. The comparisons are done based on length first (smaller entries before larger entries) and then by using compareToIgnoreCase.
import java.util.Arrays;
import java.util.Comparator;
public class Test {
// How would you write a method that takes 2 string arrays and merges them
// into one array in order by the length of each element in the array by
// using mergeSort. And by using compareToIgnoreCase?
private static Comparator<String> COMP = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1.length() < o2.length()) {
return -1;
}
if(o1.length() > o2.length()) {
return 1;
}
return o1.compareToIgnoreCase(o2);
}
};
public static String[] mergeUnsorted(String[] arr1, String[] arr2) {
arr1 = sort(arr1);
arr2 = sort(arr2);
return merge(arr1, arr2);
}
private static String[] sort(String[] arr) {
if(arr.length <= 1)
return arr;
String[] left = Arrays.copyOfRange(arr, 0, arr.length/2);
String[] right = Arrays.copyOfRange(arr, arr.length/2, arr.length);
left = sort(left);
right = sort(right);
String[] combined = merge(left, right);
return combined;
}
private static String[] merge(String[] arr1, String[] arr2) {
String[] combined = new String[arr1.length + arr2.length];
int a = 0, b = 0, i = 0;
while(a < arr1.length || b < arr2.length) {
int compare = 0;
if(a >= arr1.length) {
compare = 1;
} else if(b >= arr2.length) {
compare = -1;
} else {
compare = COMP.compare(arr1[a], arr2[b]);
}
if(compare < 0) {
combined[i] = arr1[a];
i++;
a++;
} else if(compare > 0) {
combined[i] = arr2[b];
i++;
b++;
} else {
combined[i] = arr1[a];
i++;
a++;
combined[i] = arr2[b];
i++;
b++;
}
}
return combined;
}
public static void main(String[] args) {
String[] arr1 = new String[] { "abc", "a", "A", "bA", "Ba" };
String[] arr2 = new String[] { "def", "d", "D", "fG", "Fg", "abcde", "B" };
System.out.println(Arrays.toString(mergeUnsorted(arr1, arr2)));
}
}
Upvotes: 1
Reputation: 3889
If you want to sorting you should use ArrayList. ArrayList provide sort method for String. No need to implement your custom sorting.
Further more if you want to sort your own objects Collection provide the facility to sort them using Comparable and Comparator.
List<String> ls1=new ArrayList<String>();
List<String> ls2=new ArrayList<String>();
ls1.addAll(ls2);
Collections.sort(ls1);
Upvotes: 0
Reputation: 234795
There are a couple of problems with your code. First, index1
and index2
should be int
values instead of String
values. Second, you cannot compare strings using <
; use String.compareTo()
or String.compareToIgnoreCase()
. If you want to compare string lengths, then use something like if (arr1[index1].length() < arr2[index2].length()) { ...
.
Otherwise, I think you're on the right track.
Upvotes: 0