0

I am trying to implement a simple insertion sort algorithm and make it generic to all the instance of Comparable interface.

public static <E extends Comparable<E>> void InsertionSort( E [] array)
{
    for(int i = 1; i < array.length; i++)
    {
        E current = array[i];

        int k;
        for(k = i-1; k >= 0 && current.compareTo(array[k]) < 0 ; k--)
        {
            array[k+1] = array[k];

        }

        array[k+1]=current;

    }

    for(int l = 0; l < array.length; l++)
    {
        System.out.print(array[l]+" ");

    }

    System.out.println();
}

The problem I have is I don't know the differences between

 <E extends Comparable<E>>

and

 <E extends Comparable>

They both work but I don't know the reason for the first one.

1
  • and the best option is <E extends Comparable<? super E>> Commented May 16, 2015 at 0:29

1 Answer 1

7

E extends Comparable<E> means that an instance of E can be compared to other objects of type E.

E extends Comparable means that an instance of E can be compared to...something. It's a raw type, which is bad. Don't do that.

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

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.