Help with Comparable interface.
Hi,
I have written a program to implement five sorting methods. This program will sort an array obtained from user input and return the sorted array along with the number of comparisons and total time of execution of each method. When I try to compile, I receive errors stating "int cannot be dereferenced."
The following is the code for which I am receiving the error.
Code :
public static <numbers extends Comparable> int mergeSort (int numbers[], int min, int max)
{
int temp[];
int index1, left, right;
//find the length and the midpoint of the list
int size = max - min + 1;
int pivot = (min + max) / 2;
temp = (int[])(new Comparable[size]);
//sort left half of list
mergeSort(numbers, min, pivot);
//sort right half of list
mergeSort(numbers, pivot + 1, max);
//copy sorted data into workspace
for (index1 = 0; index1 < size; index1++)
temp[index1] = numbers[min + index1];
//merge the two sorted lists
left = 0;
right = pivot - min + 1;
int comparisons = 0;
for (index1 = 0; index1 < size; index1++)
{
comparisons++;
if (right <= max - min)
if (left <= pivot - min)
if (temp[left].compareTo(temp[right]) > 0)
numbers[index1 + min] = temp[right++];
else
numbers[index1 + min] = temp[left++];
else
numbers[index1 + min] = temp[right++];
else
numbers[index1 + min] = temp[left++];
}
return comparisons;
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using a bubble sort
// algorithm.
//-----------------------------------------------------------------
public static <numbers extends Comparable> int bubbleSort (int numbers[])
{
int position, scan;
int temp;
int comparisons = 0;
for (position = numbers.length - 1; position >= 0; position--)
{
for (scan = 0; scan <= position - 1; scan++)
{
comparisons++;
if (numbers[scan].compareTo(numbers[scan+1]) > 0)
{
// Swap the values
temp = numbers[scan];
numbers[scan] = numbers[scan + 1];
numbers[scan + 1] = temp;
}
}
}
return comparisons;
}
//-----------------------------------------------------------------
// Sorts the specified array of integers using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static <numbers extends Comparable> int selectionSort (int numbers[])
{
int min;
int temp;
int comparisons = 0;
for (int index = 0; index < numbers.length-1; index++)
{
min = index;
comparisons++;
for (int scan = index+1; scan < numbers.length; scan++)
if (numbers[scan].compareTo(numbers[min])<0)
min = scan;
// Swap the values
temp = numbers[min];
numbers[min] = numbers[index];
numbers[index] = temp;
}
return comparisons;
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using an insertion
// sort algorithm.
//-----------------------------------------------------------------
public static <numbers extends Comparable> int insertionSort (int numbers[])
{
int comparisons = 0;
for (int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;
comparisons++;
// Shift larger values to the right
while (position > 0 && numbers[position-1].compareTo(key) > 0)
{
numbers[position] = numbers[position-1];
position--;
}
numbers[position] = key;
}
return comparisons;
}
I am not sure what I'm doing wrong. I am very new to java and this is the first time I am using the Comparable interface and don't believe I am doing it right. Any help is greatly appreciated. Thank you.
Re: Help with Comparable interface.
What line is giving you the error? If you want help, you should provide an SSCCE (that means you should boil the problem down to as few lines as possible) that demonstrates the problem.
Hint: int is a primitive, not an Object, so you can't do .anything() on it.