dol*_*uea 0 java sorting generics algorithm
我已经用通用方法使用参数对排序算法Quicksort进行了编码:
T[] array, int low, int high)T[] array, int low, int high)但是,当我尝试在排序方法主体中进行递归时,对于array参数,它说
Wrong 1st arguement type. Found: 'T[]', required: 'T[]'
这是排序方法中的代码:
if (low < high)
{ int pi = partition(array, low, high);
Quicksort(array, low, pi-1);
Quicksort(array, pi+1, high);
}
Run Code Online (Sandbox Code Playgroud)
这是分区方法中的代码:
T pivot = array[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
if (array[j].compareTo(pivot) <=0)
{
i++;
// swap array[i] and array[j]
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// swap array[i+1] and array[high] (or pivot)
T temp = array[i+1];
array[i+1] = array[high];
array[high] = temp;
return i+1;
Run Code Online (Sandbox Code Playgroud)
我很困惑如何尝试解决编译器错误。我尝试将其强制转换为(T)array,但它表示的是同一件事。在我看来,它希望参数的array[index]形式正确,但这会使我的方法效率低下。
有什么建议可以解决这个错误吗?
这是我的完整代码:
public class DD_ObjectBinarySearcher<T> {
//comparison count for Binary search
static int binarycount = 0;
//comparison count for Sequential search
static int seqcount = 0;
//comparison totals for calculating averages
static int stotal; static int btotal;
/**
*
* @return total counts of Sequential Search
*/
public static int getStotal() {
return stotal;
}
/**
*
* @return total counts of Binary Search
*/
public static int getBtotal() {
return btotal;
}
/**
* @param array array to be sorted
* @param low starting index
* @param high ending index
* @return partition for quick sort
*/
static <T extends Comparable<T>> int partition(T[] array, int low, int high)
{
T pivot = array[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
if (array[j].compareTo(pivot) <=0)
{
i++;
// swap array[i] and array[j]
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// swap array[i+1] and array[high] (or pivot)
T temp = array[i+1];
array[i+1] = array[high];
array[high] = temp;
return i+1;
}
/**
* @param array array to be sorted
* @param low starting index
* @param high ending index
*/
static <T> void Quicksort(T[] array, int low, int high)
{
if (low < high)
{ int pi = partition(array, low, high);
Quicksort(array, low, pi-1);
Quicksort(array, pi+1, high);
}
}
/**
* @param a array
* @param b compared integer
* @return flag
*/
static <T extends Comparable<T>> boolean sequentialSearch(T[] a, T b){
for (T i : a) {
if (i==b){
System.out.println("The number of comparisons for unsorted array: " + seqcount);
stotal+=seqcount;
return true;
}
seqcount++;
}
return false;
}
/**
* @param a array
* @param b compared integer
* @return flag
*/
static <T extends Comparable<T>> boolean binarySearch(T[] a, T b) {
if (a.length == 0) return false;
int low = 0;
int high = a.length-1;
while(low <= high ) {
int middle = (low+high) /2;
if (b.compareTo((T) a[middle]) > 0){
low = middle +1;
} else if (b.compareTo((T) a[middle]) < 0){
high = middle -1;
} else { // the element has been found
System.out.println("The number of comparisons for sorted array: " + binarycount);
btotal+=binarycount; //totals are used to calculate average in the main
return true;
}
binarycount++;
}
return false;
}
/**
*
* @param array that will be printed
*/
static void printArray(int[] array)
{
for (int value : array) System.out.print(value + " ");
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
这里涉及的2种不同的通用方法各自定义了一个类型参数T。将T在Quicksort没有任何界限,所以也没有必须Comparable的。但是,Tin partition的上限必须为Comparable<T>。编译器错误无法说明全部原因,但由于T范围不匹配,因此会显示错误。
使您的T界限处于Quicksort同一界限。
static <T extends Comparable<T>> void Quicksort(T[] array, int low, int high)
Run Code Online (Sandbox Code Playgroud)
通常出于灵活性考虑,我们将这一想法更进一步,因为Consumer Super(来自PECS):
static <T extends Comparable<? super T>> void Quicksort(T[] array, int low, int high)
Run Code Online (Sandbox Code Playgroud)
您应该将其添加到所有T边界,包括用于边界的边界partition以及需要边界的任何其他边界。
因为您的所有方法都是static,所以T甚至没有使用该类上定义的方法。您可以安全地删除它。
class DD_ObjectBinarySearcher {
Run Code Online (Sandbox Code Playgroud)