Home > front end >  Do I need to specify a parameter type generic when dealing with Comparable interface?
Do I need to specify a parameter type generic when dealing with Comparable interface?

Time:01-08

I have a selection sort algorithm that will work for any data type that implements the comparable interface (e.g. Integer, Double, Character).

public class SelectionSort {
    public static void sort(Comparable[] a) {
        int n = a.length;

        for (int i = 0; i < n; i  ) {
            int min = i;

            for (int j = i; j < n; j  )
                if (less(a[j], a[min]))
                    min = j;

            exchange(a, i, min);
        }
    }

    private static boolean less(Comparable a, Comparable b) {
        return a.compareTo(b) < 0;
    }

    private static void exchange(Comparable[] a, int i, int j) {
        Comparable temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static boolean isSorted(Comparable[] a) {
        for (int i = 1; i < a.length; i  )
            if (less(a[i], a[i - 1]))
                return false;

        return true;
    }
}

However, I get a multiple warnings telling me that "Comparable is a raw type. References to generic type Comparable should be parameterized". Do I need to implement the class as a generic or can I leave it as it is? If I need to implement the class as a generic, how can I do it, as the methods are static.

CodePudding user response:

It is a non Generic Class with Generic Methods.

public class SelectionSort {
    public static <T extends Comparable > void sort(T[] a) {
        int n = a.length;

        for (int i = 0; i < n; i  ) {
            int min = i;

            for (int j = i; j < n; j  )
                if (less(a[j], a[min]))
                    min = j;

            exchange(a, i, min);
        }
    }

    private static <T extends Comparable> boolean less(T a, T b) {
        return a.compareTo(b) < 0;
    }

    private static <T extends Comparable> void exchange(T[] a, int i, int j) {
        T temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static <T extends Comparable> boolean isSorted(T[] a) {
        for (int i = 1; i < a.length; i  )
            if (less(a[i], a[i - 1]))
                return false;

        return true;
    }

    public static void main(String[] args) {
        Integer [] arr = {4,9,5,2,67};
        SelectionSort.sort(arr);
        for(int j=0; j<arr.length; j  ){
            System.out.println(arr[j]);
        }
    }
}

This code will throw Exception if you call sort method on an object whose class does not implement Comparable interface

CodePudding user response:

here's the updated code:

public class SelectionSort {
public static <T extends Comparable<T>> void sort(T[] a) {
    int n = a.length;

    for (int i = 0; i < n; i  ) {
        int min = i;

        for (int j = i; j < n; j  )
            if (less(a[j], a[min]))
                min = j;

        exchange(a, i, min);
    }
}

private static <T extends Comparable<T>> boolean less(T a, T b) {
    return a.compareTo(b) < 0;
}

private static <T extends Comparable<T>> void exchange(T[] a, int i, int j) {
    T temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

public static <T extends Comparable<T>> boolean isSorted(T[] a) {
    for (int i = 1; i < a.length; i  )
        if (less(a[i], a[i - 1]))
            return false;

    return true;
}

}

Here's how you can use your Selection sort now:

Integer[] intArray = {5, 4, 3, 2, 1};
SelectionSort.sort(intArray);
  • Related