I know how to write out quickSort but I have no clue on how to implement <E exte
ID: 3676621 • Letter: I
Question
I know how to write out quickSort but I have no clue on how to implement <E extends Comparable<E>>.
Here is my code. I keep getting the error that Eclipse does not recognize CompareTo. I tried instantiating a ArrayList<E extends Comparable<E>>, but that does not work.
If you could explain how to do this and show how it is done? Thank You.
import java.util.ArrayList;
import java.util.Random;
public class Assignment2 {
static ArrayList inputList;
static Random r = new Random();
public static void swap(int IndexOfNumber1, int IndexOfNumber2){
Object temp = inputList.get(IndexOfNumber1);
inputList.set(IndexOfNumber1, inputList.get(IndexOfNumber2));
inputList.set(IndexOfNumber2, temp);
}
public static <E extends Comparable<E>> void quickSort(ArrayList<E> theList){
inputList = theList;
int start = 0;
int end = inputList.size() - 1;
quickSort2(start, end);
}
public static int partition(int inLeft, int inRight){
int left = inLeft;
int right = inRight;
int pivotIndex = r.nextInt(right-left)+left;
Object pivot = inputList.get(pivotIndex);
swap(left,pivotIndex);
left++;
while(left<right){
while ( inputList.get(left).compareTo(pivot) > 0 && right > left){
left++;
}
while (inputList.get(right).compareTo(pivot) > 0 && right > left){
right--;
}
swap(left,right);
}
right = inRight;
while(inputList.get(right).compareTo(pivot) > 0 && right > inLeft){
right--;
}
swap(inLeft, right);
return right;
}
private static void quickSort2(int left, int right){
//base case: if the left and right pointers scan the entire array, the quickSort method quits.
if (right <= left)
return;
else{
int partitionInt = partition(left, right);
quickSort2( 0, partitionInt-1);
quickSort2( partitionInt+1, right);
}
}
public static void printArray(int[] input){
for(Object obj : inputList){
System.out.print(obj.toString() + " ");
}
System.out.println();
}
public static void main(String[] arg){
}
}