4

I am implementing insertion sort method. Here is the requirement of my code.

  1. The method insertionSort is a static method that returns nothing.
  2. It has two parameters: a generic array and a Comparator (generic).
  3. 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.

Mr. Polywhirl
  • 35,562
  • 12
  • 75
  • 123
dgl
  • 73
  • 2
  • 9
  • "Is anagram" is very simple, just sort `first` and `second` and compare the strings lexicographicaly. – Mr. Polywhirl Oct 12 '14 at 22:33
  • Yes, that is the easy part.I am having trouble with implementing the sorting methods. – dgl Oct 12 '14 at 22:34
  • It shouldn't be too hard to port this [C implementation of insertion sort](http://en.wikipedia.org/wiki/Insertion_sort#List_insertion_sort_code_in_C). There is also pseudo-code on wikipedia for [Merge sort](http://en.wikipedia.org/wiki/Merge_sort#Algorithm). – Mr. Polywhirl Oct 12 '14 at 22:45
  • Yes, I am able to implement insertion sort with only passing generic array as parameter. But I am in my assignment I am required to pass a comparator as parameter as well. This where I am confused. – dgl Oct 12 '14 at 22:50

1 Answers1

7

Comparator is an interface, which can not be instantiated. You need to implement it. There are two methods to implement:

  • compare
  • equals

You need to implement them for Integer elements. Like this:

public class IntegerComparator implements Comparator {

    public int compare(Integer a, Integer b) {
        return a.intValue() - b.intValue();
    }

    public int equals(Object obj) {
        return this.equals(obj);
    }

}

and in your main you call it like this:

insertionSort(a, new IntegerComparator );

Explanation: Comparator is an interface, therefore it cannot be instantiated. You need to implement it. You have an array of Integer elements to sort, therefore you can implement an Integer Comparator. The compare method returns the subtraction of the int values. If a < b, then it is negative. If a == b, then it is 0. If a > b, then it is positive.

Read more here and here.

Lajos Arpad
  • 53,986
  • 28
  • 88
  • 159
  • What about comparator for Generic type? – dgl Oct 12 '14 at 23:57
  • 3
    @dgl, your insertionSort gets a Comparator of generic type, but you have to define your Comparator and as things are standing you cannot implement a compare method which will successfully compare two elements of any type using any representation. There are infinite possibilities and you cannot determine them in advance. You need to implement your Comparator for Integer and pass it to your method, which uses a Comparator for Generic type (the interface), but when you call it, you have to specify the type. I believe this is why the task seemed hard for you. – Lajos Arpad Oct 13 '14 at 00:44
  • Thanks, you are correct! I was trying to implement one comparator method that will work for every type; which is impossible like you said. I understand what I need to do now. – dgl Oct 13 '14 at 01:15