0

I am testing the different sort methods (Selection, bubble insertion) and I am trying to use Comparator at the same time.

So far I have two classes; Main and Selectionsort.

My main looks like this:

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] anArray = {"Ludo", "matador", "ChessTitan", "Rottefælden"};

        for (int i=0; i<anArray.length-1; i++) {
            for (int j=i+1; j<anArray.length; j++) {
                if (anArray[j].compareTo(anArray[i]) < 1) {

                    String temp = anArray[i];
                    anArray[i] = anArray[j];
                    anArray[j] = temp;

                }
            }

        }

        for (String string : anArray) {
            System.out.println(string);
        }
    }
}

And my selectionSort looks like this:

public class SelectionSort implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {      
        return o1.compareTo(o2);
    }
}

What I want to do is use my Comparator when I use selection sort.

How is this possible?

0

3 Answers 3

2

replace this line:

if (anArray[j].compareTo(anArray[i]) < 1) {

with this:

if (comparator.compare(anArray[j],anArray[i]) < 1) {

where comparator is an instance of the comparator you want to use.


Since your Comparator has no state, you'll probably want to assign it to a static final field.

private static final Comparator<String> COMP = new SelectionSort();

So now the above code would read

if (COMP.compare(anArray[j],anArray[i]) < 1) {

Here's a solution for your last question. Create two overloaded static methods, one with a Comparator, one without, then also create a Comparator that uses the natural order (amazingly no such thing is available to my knowledge in the JDK). Something like this:

private static final Comparator<? extends Comparable> NATURAL_ORDER = new Comparator<Comparable>() {
    @Override
    public int compare(final Comparable o1, final Comparable o2) {
        return o1.compareTo(o2);
    }
};

private static <T> Comparator<T> naturalOrder() {
    return (Comparator<T>) NATURAL_ORDER;
}

public static <T> void sort(final T[] array) {
    if (!Comparable.class.isAssignableFrom(array.getClass().getComponentType())) {
        throw new IllegalArgumentException(
              "Array Component Type must implement Comparable");
    }

    sort(array, naturalOrder());
}

public static <T> void sort(final T[] array, final Comparator<? super T> comparator) {
    // implement sort here
}
0
0

You can use Arrays.sort

Arrays.sort(myArray, myComparator)

Then you can pass in any comparator you want.

4
  • 2
    Array.sort is merge sort not selection sort Commented Sep 13, 2012 at 9:21
  • 1
    Good point Amit, my mistake. To quote the docs "The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance."
    – RNJ
    Commented Sep 13, 2012 at 9:22
  • So i am unable to use Arrays.sort with selection sort? Commented Sep 13, 2012 at 9:30
  • Correct. It was my mistake Marc
    – RNJ
    Commented Sep 13, 2012 at 9:39
0

You can create instance of SelectionSort to use it for comparison

SelectionSort selectionSort = new SelectionSort();
if (selectionSort.compare(anArray[i], anArray[j]) < 1) {
1
  • So far ive created an instance of my comparator class and used that in my if sentence! :) it works and i am really happy now i need to move on to bubblesort :) Commented Sep 13, 2012 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.