Java 快速排序中的比较计数

Java 快速排序中的比较计数,java,arrays,sorting,Java,Arrays,Sorting,我需要帮助计算这些功能所做的比较的数量请。我已经声明了一个变量compCount,它存储比较的数量,但是请帮助确定compCount++增量的位置 我理解您在执行.compare函数时放置count incrementors的方法,但也有类似这样的复杂循环: for (i = low, j = high - 1;;) { while (a[++i].compareTo(pivot) < 0) {

我需要帮助计算这些功能所做的比较的数量请。我已经声明了一个变量compCount,它存储比较的数量,但是请帮助确定compCount++增量的位置

我理解您在执行.compare函数时放置count incrementors的方法,但也有类似这样的复杂循环:

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);
        }
for(i=low,j=high-1;;){
while(a[++i.)。与(pivot)<0比较{
}
while(pivot.compareTo(a[--j])<0){
}
如果(i>=j){
打破
}
(a,i,j);
}
在insertionSort()函数中也是如此

代码如下:

public class QuickSort implements Sort {
long compCount;

/**
 * Quicksort algorithm.
 * 
 * @param a an array of Comparable items.
 */
@SuppressWarnings("rawtypes")
public void QuickSort(Comparable[] a) {
    compCount = 0;
    sort(a);
}

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 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.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private 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, low, middle);
        }
        if (a[high].compareTo(a[low]) < 0) {
            
            swapReferences(a, low, high);
        }
        if (a[high].compareTo(a[middle]) < 0) {
            
            swapReferences(a, middle, high);
        }
        // Place pivot at position high - 1
        swapReferences(a, middle, 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, i, high - 1);

        quicksort(a, low, 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.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void insertionSort(Comparable[] a, int low, 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;
    }
}

@SuppressWarnings("rawtypes")
@Override
public void sort(Comparable[] a) {
    // TODO Auto-generated method stub
    compCount = 0;
    quicksort(a, 0, a.length - 1);
}

@Override
public long getCompares() {
    // TODO Auto-generated method stub
    return compCount;
}
public class QuickSort implements Sort {
long compCount;

/**
 * Quicksort algorithm.
 * 
 * @param a an array of Comparable items.
 */
@SuppressWarnings("rawtypes")
public void QuickSort(Comparable[] a) {
    compCount = 0;
    sort(a);
}

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 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.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private 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, low, middle); 
        }
        compCount++;
        if (a[high].compareTo(a[low]) < 0) {
            swapReferences(a, low, high);
        }
        compCount++;
        if (a[high].compareTo(a[middle]) < 0) {
            swapReferences(a, middle, high);    
        }
        compCount++;
        // Place pivot at position high - 1
        swapReferences(a, middle, high - 1);
        Comparable pivot = a[high - 1];

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

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

        quicksort(a, low, 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.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void insertionSort(Comparable[] a, int low, 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--) {
            compCount++;
            a[j] = a[j - 1];
        }
        
        if(j > low)
            compCount++;
        a[j] = tmp;
    }
}

@SuppressWarnings("rawtypes")
@Override
public void sort(Comparable[] a) {
    // TODO Auto-generated method stub
    compCount = 0;
    quicksort(a, 0, a.length - 1);
}

@Override
public long getCompares() {
    // TODO Auto-generated method stub
    return compCount;
}
公共类快速排序实现排序{
长计数;
/**
*快速排序算法。
* 
*@param是一组可比较的项目。
*/
@抑制警告(“原始类型”)
公共无效快速排序(可比[]a){
compCount=0;
排序(a);
}
专用静态最终整数截止=10;
/**
*方法交换到数组中的元素。
* 
*@param是一个对象数组。
*@param index1第一个对象的索引。
*@param index2第二个对象的索引。
*/
公共静态参数设置(对象[]a,int index1,int index2){
对象tmp=a[index1];
a[index1]=a[index2];
a[index2]=tmp;
}
/**
*进行递归调用的内部快速排序方法。使用三个中间值
*分区和10的截止值。
* 
*@param是一组可比较的项目。
*@param low子阵列的最左侧索引。
*@param high子数组的最右侧索引。
*/
@SuppressWarnings({“rawtypes”,“unchecked”})
专用void快速排序(可比[]a,整数低,整数高){
如果(低+截止>高)
插入排序(a、低、高);
否则{
//分低、中、高
int-middle=(低+高)/2;
如果(a[中间])。与(a[低])相比<0{
SWA偏好(a、低、中);
}
如果(a[高]。与(a[低])相比<0){
SWA偏好(a、低、高);
}
如果(a[高]。与(a[中])相比<0){
SWA偏好(a、中、高);
}
//将枢轴置于位置high-1
SWA偏好(a、中、高-1);
可比支点=a[高-1];
//开始分区
int i,j;
对于(i=低,j=高-1;;){
while(a[++i.)。与(pivot)<0比较{
}
while(pivot.compareTo(a[--j])<0){
}
如果(i>=j){
打破
}
(a,i,j);
}
//恢复枢轴
SWA偏好(a、i、高-1);
快速排序(a,low,i-1);//对小元素进行排序
快速排序(a,i+1,高);//对大型元素进行排序
}
}
/**
*快速排序使用的子数组的内部插入排序例程。
* 
*@param是一组可比较的项目。
*@param low子阵列的最左侧索引。
*@param n要排序的项目数。
*/
@SuppressWarnings({“unchecked”,“rawtypes”})
专用void insertionSort(可比[]a,整数低,整数高){
对于(int p=low+1;p low&&tmp.compareTo(a[j-1])<0;j--){
a[j]=a[j-1];
}
a[j]=tmp;
}
}
@抑制警告(“原始类型”)
@凌驾
公共无效排序(可比[]a){
//TODO自动生成的方法存根
compCount=0;
快速排序(a,0,a.长度-1);
}
@凌驾
公共长getCompares(){
//TODO自动生成的方法存根
返回compCount;
}

}好的,多亏了我的朋友,我才弄明白。事实证明,您应该计算if语句外部的比较,而不是内部的比较,我还忽略了一些条件:

代码如下:

public class QuickSort implements Sort {
long compCount;

/**
 * Quicksort algorithm.
 * 
 * @param a an array of Comparable items.
 */
@SuppressWarnings("rawtypes")
public void QuickSort(Comparable[] a) {
    compCount = 0;
    sort(a);
}

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 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.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private 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, low, middle);
        }
        if (a[high].compareTo(a[low]) < 0) {
            
            swapReferences(a, low, high);
        }
        if (a[high].compareTo(a[middle]) < 0) {
            
            swapReferences(a, middle, high);
        }
        // Place pivot at position high - 1
        swapReferences(a, middle, 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, i, high - 1);

        quicksort(a, low, 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.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void insertionSort(Comparable[] a, int low, 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;
    }
}

@SuppressWarnings("rawtypes")
@Override
public void sort(Comparable[] a) {
    // TODO Auto-generated method stub
    compCount = 0;
    quicksort(a, 0, a.length - 1);
}

@Override
public long getCompares() {
    // TODO Auto-generated method stub
    return compCount;
}
public class QuickSort implements Sort {
long compCount;

/**
 * Quicksort algorithm.
 * 
 * @param a an array of Comparable items.
 */
@SuppressWarnings("rawtypes")
public void QuickSort(Comparable[] a) {
    compCount = 0;
    sort(a);
}

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 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.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private 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, low, middle); 
        }
        compCount++;
        if (a[high].compareTo(a[low]) < 0) {
            swapReferences(a, low, high);
        }
        compCount++;
        if (a[high].compareTo(a[middle]) < 0) {
            swapReferences(a, middle, high);    
        }
        compCount++;
        // Place pivot at position high - 1
        swapReferences(a, middle, high - 1);
        Comparable pivot = a[high - 1];

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

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

        quicksort(a, low, 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.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void insertionSort(Comparable[] a, int low, 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--) {
            compCount++;
            a[j] = a[j - 1];
        }
        
        if(j > low)
            compCount++;
        a[j] = tmp;
    }
}

@SuppressWarnings("rawtypes")
@Override
public void sort(Comparable[] a) {
    // TODO Auto-generated method stub
    compCount = 0;
    quicksort(a, 0, a.length - 1);
}

@Override
public long getCompares() {
    // TODO Auto-generated method stub
    return compCount;
}
公共类快速排序实现排序{
长计数;
/**
*快速排序算法。
* 
*@param是一组可比较的项目。
*/
@抑制警告(“原始类型”)
公共无效快速排序(可比[]a){
compCount=0;
排序(a);
}
专用静态最终整数截止=10;
/**
*方法交换到数组中的元素。
* 
*@param是一个对象数组。
*@param index1第一个对象的索引。
*@param index2第二个对象的索引。
*/
公共静态参数设置(对象[]a,int index1,int index2){
对象tmp=a[index1];
a[index1]=a[index2];
a[index2]=tmp;
}
/**
*进行递归调用的内部快速排序方法。使用三个中间值
*分区和10的截止值。
* 
*@param是一组可比较的项目。
*@param low子阵列的最左侧索引。
*@param high子数组的最右侧索引。
*/
@SuppressWarnings({“rawtypes”,“unchecked”})
专用void快速排序(可比[]a,整数低,整数高){
如果(低+截止>高){
插入排序(a、低、高);
}
否则{
//分低、中、高
int-middle=(低+高)/2;
如果(a[中间])。与(a[低])相比<0{
SWA偏好(a、低、中);
}
compCount++;
如果(a[高]。与(a[低])相比<0){
SWA偏好(a、低、高);
}
compCount++;
如果(a[高]。与(a[中])相比<0){
SWA偏好(a、中、高);
}
compCount++;
//将枢轴置于位置high-1
SWA偏好(a、中、高-1);
可比支点=a[高-1];
//开始分区
int i,j;
对于(i=低,j=高-1;;){
while(a[++i.)。与(pivot)<0比较{
compCount++;
}
compCount++;
while(pivot.比较[