繁体   English   中英

Java中的QuickSort和PivotType枚举问题

[英]QuickSort and PivotType enum issue in Java

在这堂课上

PivotType错误:公共静态枚举QuickSort.PivotType扩展了java.lang.Enum {

下面是我的代码语法

public class QuickSort<T extends java.lang.Comparable<? super T>> extends RunTime implements SortInterface<T>
    {


    public static enum QuickSort.PivotType  extends java.lang.Enum<QuickSort.PivotType> {


        public static final PivotType FirstElement, RandomElement, MidOfFirstMidLastElement;
    }
        public static QuickSort.PivotType[] values()
        {
            QuickSort.PivotType[] pt = new QuickSort.PivotType[3];
            int index = 0;
            for (QuickSort.PivotType c : QuickSort.PivotType.values())
                pt[index++] = c;

            return pt;
        }

    public static QuickSort.PivotType valueOf(String name) throws IllegalArgumentException, NullPointerException
    {
        if (name == null)
            throw new NullPointerException();
        else if (name.equals("First"))
            return FirstElement;
        else if (name.equals("Random"))
            return RandomElement;
        else if (name.equals("Mid"))
            return MidOfFirstMidLastElement;
        else
            throw new IllegalArgumentException();
    }
}



    private QuickSort.PivotType pivotType;

    public QuickSort() {
        pivotType = FirstElement;
    }

    public QuickSort.PivotType getPivotType() {
        return pivotType;
    }

    public void setPivotType(QuickSort.PivotType pivotType) {
        this.pivotType = pivotType;
    }

    public void sort(T[] array) {

        long startTime = System.currentTimeMillis();
        quickSort(array, 0, array.length - 1);

        long endTime   = System.currentTimeMillis();
        long totalTime = endTime - startTime;

        addRuntime(totalTime);
    }

    private void quickSort(T[] array, int firstIndex, int lastIndex) {
        int pivotIndex;
        if (firstIndex < lastIndex) {
            pivotIndex = partition(array, firstIndex, lastIndex);
            quickSort(array, firstIndex, pivotIndex - 1);
            quickSort(array, pivotIndex + 1, lastIndex);
        }
    }

    public void choosePivot(T[] array, int first, int last) {
        // after this method returns, the chosen pivot is
        // always in the first location in the segment of the
        // array being sorted
        return;
    }



    private int partition(T[] array, int first, int last) {
        // ---------------------------------------------------------
        // Partitions an array for quicksort.
        // Precondition: theArray[ first ... last] where first <= last.
        // Postcondition: Returns the index of the pivot element of
        // theArray[ first ... last]. Upon completion of the method,
        // this will be the index value lastS1 such that
        // S1 = theArray[first ... lastS1-1] < pivot
        // theArray[lastS1] == pivot
        // S2 = theArray[lastS1+1 ... last] >= pivot
        // Calls: choosePivot.
        // ---------------------------------------------------------

        // tempItem is used to swap elements in the array
        T tempItem;

        // place pivot in theArray[first]

        choosePivot(array, first, last);
        T pivot = array[first]; // reference pivot

        // initially, everything but pivot is in unknown
        int lastS1 = first; // index of last item in S1

        // move one item at a time until unknown region is empty
        for (int firstUnknown = first + 1; firstUnknown <= last; firstUnknown++) {
            // Invariant: theArray[first+1 ... lastS1] < pivot

            // move item from unknown to proper region
            if (array[firstUnknown].compareTo(pivot) < 0) {
                // item from unknown belongs in S1
                ++lastS1;
                tempItem = array[firstUnknown];
                array[firstUnknown] = array[lastS1];
                array[lastS1] = tempItem;
            } // end if
            // else item from unknown belongs in S2
        } // end for

        // place pivot in proper position and mark its location
        tempItem = array[first];
        array[first] = array[lastS1];
        array[lastS1] = tempItem;

        return lastS1;
    } // end partition
}

用下面的代码替换您的枚举代码。 这将使您更接近编译。

   public enum PivotType {
        FirstElement,RandomElement,MidOfFirstMidLastElement;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM