Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的快速排序和数据透视类型枚举问题_Java_Enums - Fatal编程技术网

Java中的快速排序和数据透视类型枚举问题

Java中的快速排序和数据透视类型枚举问题,java,enums,Java,Enums,在这门课上,我得到了很多 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.Pi

在这门课上,我得到了很多

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;
    }

欢迎使用SO。代码不能按照发布的代码进行操作。请发布