在此特定的快速排序算法中查找比较数

我有一个用于Quick-Sort的算法,我想返回其中的比较数,但是我无法使用给定的算法做任何事情。我必须返回比较形式的getcompares()函数。我已经在线检查了很多地方,但没有找到适当的解决方案。没有的比较  已经排序的大小为100的数据应为488。

/**
 * Quicksort algorithm.
 * @param a an array of Comparable items.
 */

public static void QuickSort( Comparable [ ] a )

{
    quicksort( a,a.length - 1 );
}

private static final int CUTOFF = 10;

/**
 * Method to swap to elements in an array.
 * @param a an array of objects.
 * @param index1 the index of the first object.
 * @param index2 the index of the second object.
 */
public static final void swapReferences( Object [ ] a,int index1,int index2 )
{
    Object tmp = a[ index1 ];
    a[ index1 ] = a[ index2 ];
    a[ index2 ] = tmp;
}

/**
 * Internal quicksort method that makes recursive calls.
 * Uses median-of-three partitioning and a cutoff of 10.
 * @param a an array of Comparable items.
 * @param low the left-most index of the subarray.
 * @param high the right-most index of the subarray.
 */
private static void quicksort( Comparable [ ] a,int low,int high )
{
    if( low + CUTOFF > high )
        insertionSort( a,low,high );
    else
    {
            // Sort low,middle,high
        int middle = ( low + high ) / 2;
        if( a[ middle ].compareTo( a[ low ] ) < 0 )
            swapReferences( a,middle );
        if( a[ high ].compareTo( a[ low ] ) < 0 )
            swapReferences( a,high );
        if( a[ high ].compareTo( a[ middle ] ) < 0 )
            swapReferences( a,high );

            // Place pivot at position high - 1
        swapReferences( a,high - 1 );
        Comparable pivot = a[ high - 1 ];

            // Begin partitioning
        int i,j;
        for( i = low,j = high - 1; ; )
        {
            while( a[ ++i ].compareTo( pivot ) < 0 )
                ;
            while( pivot.compareTo( a[ --j ] ) < 0 )
                ;
            if( i >= j )
                break;
            swapReferences( a,i,j );
        }

            // Restore pivot
        swapReferences( a,high - 1 );

        quicksort( a,i - 1 );    // Sort small elements
        quicksort( a,i + 1,high );   // Sort large elements
    }
}

/**
 * Internal insertion sort routine for subarrays
 * that is used by quicksort.
 * @param a an array of Comparable items.
 * @param low the left-most index of the subarray.
 * @param n the number of items to sort.
 */
private static void insertionSort( Comparable [ ] a,int high )
{
    for( int p = low + 1; p <= high; p++ )
    {
        Comparable tmp = a[ p ];
        int j;

        for( j = p; j > low && tmp.compareTo( a[ j - 1 ] ) < 0; j-- )
            a[ j ] = a[ j - 1 ];
        a[ j ] = tmp;
    }
}
cad1234 回答:在此特定的快速排序算法中查找比较数

实现此目的的最简单方法是创建一个内部方法,用于比较2个Comparable对象的计数(计算该对象被调用的次数),并将所有compareTo调用替换为对该方法的调用

private int comparisons = 0;

private int compare(Comparable<?> obj1,Comparable<?> obj2) {
    comparisons++;
    return obj1.compareTo(obj2);
}

然后将tmp.compareTo(a[j-1])之类的表达式替换为compare(tmp,a[j-1])

您的getCompares方法将返回comparisons

要考虑的其他一些提示:

理想情况下,不要使用静态方法和变量。制作一个QuickSort类,该类在使用前需要实例化。

最好不要使用原始类型(例如代码中的Comparable)。大多数现代IDE都会正确警告这种危险。使用通配符(如上所述),或者更好的方法是,将通用类型实际上添加到您的QuickSort类中:

class QuickSort<T extends Comparable<T>> {
    public void sort(T[] array) { ... }
}

QuickSort<Integer> intSorter = new QuickSort<>();
int[] array = {5,8,2};
intSorter.sort(array);

这将允许编译时检查您没有混合使用不兼容的类型。您当前的原始类型将不允许Java执行此操作。

本文链接:https://www.f2er.com/3024928.html

大家都在问