I am implementing insertion sort method. Here is the requirement of my code.
- The method insertionSort is a static method that returns nothing.
- It has two parameters: a generic array and a Comparator (generic).
- It sorts the generic array using the merge sort algorithms
My Question is: What do I use for Comparator parameter c when calling in main method?
Here is what I have so far, I have some unimplemented method (merge sort an isAnagaram) ignore those
public class Sorting
{
public static <T extends Comparable<T>> void insertionSort(T[] a, Comparator<T> c)
{
for (int i = 0; i < a.length; i++)
{
T key = a[i];
int j;
for (j = i - 1; j >= 0; j--)
{
if (c.compare(a[j], key) <= 0)
break;
a[j + 1] = a[j];
}
a[j + 1] = key;
}
}
public static void mergeSort()
{
//TODO
}
public static boolean isAnagram(String first, String second)
{
//TODO
return false;
}
public static void main(String[] args)
{
Integer a[] = { 99, 8, 19, 88, 62, 2, 1, 9, 19 };
// not sure how to pass parameter comparator
insertionSort(a, null );
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
}
}
I looked around on stack overflow as well as googled a lot on a Comparator interface but I couldn't really find any method where you are required to pass Generic comparator as parameter. Can someone help me tell what I am not understanding or direct me to right direction.