通过通用快速排序

时间:2016-11-09 15:56:20

标签: java generics quicksort

我对传球非常困惑。我在eclipse中创建了一个快速排序算法。该类是一个抽象类。这是Interface类。

public interface ArraySort<T extends Comparable<T>>
{
    /**
     * Sort the array
     */
    public void sort(T[] array);

}

这是创建快速排序的类。

public class QuickSort <T extends Comparable<T>> extends ArraySortTool<T>{
    public <T> void quickSort(T[] array, Comparator<T>com, int a, int b) {
        if(a >= b) return;
        int left = a;
        int right = b-1;
        T pivot = array[b];
        T temp;

        while (left <= right){

            //Look for element larger or equal to the pivot
            while(left <= right&&com.compare(array[left], pivot)<0)left++;
            //Look for element smaller or equal to pivot
            while(left <= right&&com.compare(array[right], pivot)>0)right--;
            if(left <= right){
                temp = array[left]; array[right]=array[right]=temp;
                left++; right--;
            }
        }

        //place pivot into its final location marked by left index
        temp = array[left]; array[left] = array[b]; array[b] = temp;

        quickSort(array, com, a, left - 1);
        quickSort(array, com, left + 1,b);

    }

    @Override
    public void sort(T[] array) {
        quickSort(array, int, 0, 0);


    }
}

为了传递参考资料,我也试过这种方法,但没有运气。

@Override
    public void sort(T[] array, Comparator<T>com, int a, int b) {
        int left = a;
        int right = b-1;
        T pivot = array[b];
        T temp;

我在这里收到错误

public class QuickSort <T extends Comparable<T>> extends ArraySortTool<T>{

我试图在不干扰接口类的情况下这样做。

以下是ArraySortTool的代码

public abstract class ArraySortTool<T extends Comparable<T>> implements ArraySort<T>
{
    /**
     * @param inArray an array to be sorted
     * @return the time, in milliseconds, taken to sort the array
     */
    private double timeTakenMillis(T[] array) {
        double startTime = System.nanoTime();
        sort(array);
        return ((System.nanoTime()-startTime)/1000000.0);
    }

    /**
     * Run a sequence of tests on sets of arrays of increasing size, reporting the average time taken for each
     * size of array. For each size of array, <tt>noPerSize</tt> tests will be run, and the average time taken.  
     * Timings will be generated for array sizes 1,2,...,9,10,20,...,90,100,200,...,900,1000,2000,...until the
     * maximum time is exceeded.  Times are reported in milliseconds. 
     * @param generator an array generator for generating the random arrays
     * @param noPerSize the number of timings per array size set
     * @param maxTimeSeconds the cut-off time in seconds - once a timing takes longer than this the timing sequence will be terminated
     */
    public void timeInMillis(RandomArray<T> generator,int noPerSize,int maxTimeSeconds)
    {
        int size = 1;  // initial size of array to test
        int step = 1;  // initial size increase
        int stepFactor = 10; // when size reaches 10*current size increase step size by 10
        double averageTimeTaken;
        do {
            double totalTimeTaken = 0;
            for (int count = 0; count < noPerSize; count++) {
                T[] array = generator.randomArray(size);
                totalTimeTaken += timeTakenMillis(array);
            }
            averageTimeTaken = totalTimeTaken/noPerSize;
            System.out.format("Average time to sort %d elements was %.3f milliseconds.\n",size,averageTimeTaken);
            size += step;
            if (size >= stepFactor*step) step *= stepFactor;        
        } while (averageTimeTaken < maxTimeSeconds*1000);
        System.out.println("Tests ended.");
    }

    /**
     * Check whether a given array is sorted.
     * @param array the array to be checked
     * @return true iff the array is sorted - either ascending or descending
     * The first non-equal neighbouring elements will determine the expected
     * order of sorting.
     */
    public boolean isSorted(T[] array) {
        int detectedDirection = 0; // have not yet detected increasing or decreasing
        T previous = array[0];
        for (int index = 1; index < array.length; index++) {
            int currentDirection = previous.compareTo(array[index]); // compare previous and current entry
            if (currentDirection != 0) { // if current pair increasing or decreasing
                if (detectedDirection == 0) { // if previously no direction detected
                    detectedDirection = currentDirection; // remember current direction
                } else if (detectedDirection * currentDirection < 0) { // otherwise compare current and previous direction
                    return false; // if they differ array is not sorted
                }
            }
            previous = array[index];
        }
        // reached end of array without detecting pairs out of order
        return true;
    }
}

我正在尝试将quicksort方法传递给sort方法,就像在接口类中一样。请让我知道如何做到这一点,因为我是新来的参考。使用我的代码的一个例子很棒。亲切的问候。

1 个答案:

答案 0 :(得分:0)

试试这个:

public class QuickSort <T extends Comparable<T>> implements ArraySort<T>{...}

(编辑以匹配问题中的修订代码)