有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法获得正确的插入排序键比较数

我正在努力解决插入排序键比较和交换计数的数量问题

我有一种方法,可以在最后计算并打印出关键的比较结果

public static void insertionSort(int[] array) {

    int n = array.length;
    int cm = 0;
    int sw = 0;


    for (int pass = 0; pass < array.length; pass++) {
        // figure out what should go into a[pass]
        int min = pass;
        for (int j = pass + 1; j < array.length; j++) {
            if (smaller(array, j, min)) {
                cm++;
                min = j;
            }
        }
        swap(array, pass, min);
        sw++;
    }

    System.out.print("Insertion sort: ");
    for (int c = 0; c < n; c++) {
        System.out.print(array[c] + " ");


    }

    System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}

private static void swap(int[] a, int i, int j) {
    if (i != j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

private static boolean smaller(int[] a, int i, int j) {
    //another suggestion came up to call a count variable here because a false comparison could still count as a comparison
    count++;
    if (a[i] < a[j]) {
        return true;
    }
    return false;
}

用这个测试阵列

int[] test = {13, 12, 5, 6, 11};

我应该得到7比较和4交换,但我得到了5比较和5交换。 使用另一个从031的数组(测试最佳情况), 我得到0比较和32交换


共 (1) 个答案

  1. # 1 楼答案

    更新以获取答案。 这适用于比较计数,但仍适用于交换计数

    private static int COMPCOUNT = 0;
    public static void main(String[] args) {
        //best case for insertion sort is increasing order
        int[] bestCase = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
        //the worst case for insertion sort is decreasing order;
        int[] worstCase = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    
        int[] randomArray = new int[32];
        for (int i = 0; i < randomArray.length; i++) {
            randomArray[i] = genarateRandom(32);
        }
    }
    
    public static int genarateRandom(int bound) {
        Random random = new Random();
        int rand = random.nextInt(bound);
        return rand;
    }
    
    private static boolean smaller(int[] a, int i, int j) {
        if (a[i] < a[j]) {
            COMPCOUNT++;
            return true;
        }
        return false;
    }
    
    
    public static void insertionSort(int[] arr)
    {
        COMPCOUNT=0;
        int temp;
        for (int i = 1; i < arr.length; i++) {
            for(int j = i ; j > 0 ; j ){
                //use boolean function to check A[i] < A[j]
                if(smaller(arr, j, j-1)){
                    //swap
                    temp = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = temp;
                }
            }
        }
    
        //print out the array
        System.out.print("Insertion sort: ");
        for (int c = 0; c < arr.length; c++) {
            System.out.print(arr[c] + " ");
    
        }
    
        //print out the number of comparison
        System.out.println("- " + COMPCOUNT + " comparisons");
    }