0

I know this is a very sloppy quicksort, but it is the one I am required to use.

My question is how to change the type of the y array to type E on line x[k+i]=(E) (y[k]). The way I'm currently doing does not cast object as type E. The quicksort works fine (at least before the recursion), it's just the casting at this point.

public class quicksort {
    static <E extends Comparable<E>> E[] quickSort(E[] x, int i, int j) {
        int pivot = 0;
        Object[] y = new Object[j - i + 1];
        for (int k = 0; k < y.length; k++) y[k] = x[k + i];
        int lput = i;
        int rput = j;
        for (int k = i + 1; k <= j; k++) {
            if (x[k].compareTo(x[pivot]) < 0) {
                y[(lput++) - i] = x[k];
            } else {
                y[(rput--) - i] = x[k];
            }
        }

        pivot = lput;
        y[lput - i] = pivot;
        for (int k = 0; k < y.length; k++)
            x[k + i] = (E) (y[k]); // this line
        quickSort(x, i, pivot - 1);
        quickSort(x, pivot + 1, j);
        System.out.println("got here");
        return x;
    }
}

Here is the stack trace. the line refer to is x[k + i] = (E) (y[k]);

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
Type mismatch: cannot convert from Object to E

at quicksort.quickSort(quicksort.java:23)
at ArrayListGui$6.actionPerformed(ArrayListGui.java:158)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
user1803551
  • 11,914
  • 5
  • 39
  • 63

2 Answers2

0

What you have is already the most correct way to do it. It compiles and works fine.

An alternative way would be to create the array as:

E[] y = (E[])new Comparable[j - i + 1];

and then when you get it out, you wouldn't have to cast it:

x[k + i] = (E) (y[k]);

However, this is kind of dangerous because the actual runtime class of the object pointed to y is Comparable[], not E[], and if you accidentally expose y to the outside of the method as type E[], it can lead to crashes in other pieces of code, which is unexpected. In this method, you are not exposing y to the outside of the method, so it won't cause any problems in this case.

newacct
  • 110,405
  • 27
  • 152
  • 217
-2

You can do it by passing a type token, which can then be used to create an instance of a typed using the JDK's Array.newInstance(Class<?> componentType, int length) factory method:

static <E extends Comparable<E>> E[] quickSort(Class<E> clazz, E[] x, int i, int j) {
    E[] y = (E[]) Array.newInstance(clazz, j - i + 1);
    ...
}
Bohemian
  • 365,064
  • 84
  • 522
  • 658