Java 合并排序和选择排序的计数操作

Java 合并排序和选择排序的计数操作,java,algorithm,sorting,mergesort,Java,Algorithm,Sorting,Mergesort,我想比较排序算法Merge-Sort和Selection-Sort的操作计数,但是我在计算哪些操作要计数,哪些不需要计数时遇到了一些问题 下面是我的实现。我认为我以正确的方式计算了选择排序的操作,但我不知道合并排序: public class Mergesort { private int[] numbers; private int[] helper; private int number; private int comparisons, exchanges; public void s

我想比较排序算法Merge-Sort和Selection-Sort的操作计数,但是我在计算哪些操作要计数,哪些不需要计数时遇到了一些问题

下面是我的实现。我认为我以正确的方式计算了选择排序的操作,但我不知道合并排序:

public class Mergesort {
private int[] numbers;
private int[] helper;

private int number;
private int comparisons, exchanges;

public void sort(int[] values) {
    this.numbers = values;
    number = values.length;
    this.helper = new int[number];
    mergesort(0, number - 1);
    System.out.println("MerSort_Comparisons "+comparisons);
    System.out.println("MerSort_Exchanges "+exchanges);
    System.out.println("MerSort_Operations "+(comparisons+exchanges));
    System.out.println();
}

private void mergesort(int low, int high) {
    // Check if low is smaller then high, if not then the array is sorted
    if (low < high) 
    {
        // Get the index of the element which is in the middle
        int middle = (low + high) / 2;
        // Sort the left side of the array
        mergesort(low, middle);
        // Sort the right side of the array
        mergesort(middle + 1, high);
        // Combine them both
        merge(low, middle, high);
    }
}

private void merge(int low, int middle, int high) {

    // Copy both parts into the helper array
    for (int i = low; i <= high; i++) {
        helper[i] = numbers[i];
        exchanges++;
    }
    int i = low;
    int j = middle + 1;
    int k = low;
    // Copy the smallest values from either the left or the right side back
    // to the original array
    while (i <= middle && j <= high) {
        if (helper[i] <= helper[j]) {
            numbers[k] = helper[i];
            i++;
            exchanges++;
        } else {
            numbers[k] = helper[j];
            j++;
            exchanges++;
        }
        k++;
        comparisons++;

    }
    // Copy the rest of the left side of the array into the target array
    while (i <= middle) {
        numbers[k] = helper[i];
        exchanges++;
        k++;
        i++;
    }
}   
}
选择排序:

public class SelectionSort {
private int exchanges, comparisons;

public void selectionSort(int[] x) {    
    for (int i=0; i<x.length-1; i++) {
        for (int j=i+1; j<x.length; j++) {
            if (x[i] > x[j]) 
            {
                //... Exchange elements
                int temp = x[i];
                x[i] = x[j];
                x[j] = temp;
                exchanges++;
            }
            comparisons++;
        }
    }
    System.out.println("SelSort_Exchanges: "+exchanges);
    System.out.println("SelSort_Comparisons: "+comparisons);
    System.out.println("SelSort_Operations: "+(exchanges+comparisons));
}
}
对我来说似乎是对的,但现在对于合并排序:

public class Mergesort {
private int[] numbers;
private int[] helper;

private int number;
private int comparisons, exchanges;

public void sort(int[] values) {
    this.numbers = values;
    number = values.length;
    this.helper = new int[number];
    mergesort(0, number - 1);
    System.out.println("MerSort_Comparisons "+comparisons);
    System.out.println("MerSort_Exchanges "+exchanges);
    System.out.println("MerSort_Operations "+(comparisons+exchanges));
    System.out.println();
}

private void mergesort(int low, int high) {
    // Check if low is smaller then high, if not then the array is sorted
    if (low < high) 
    {
        // Get the index of the element which is in the middle
        int middle = (low + high) / 2;
        // Sort the left side of the array
        mergesort(low, middle);
        // Sort the right side of the array
        mergesort(middle + 1, high);
        // Combine them both
        merge(low, middle, high);
    }
}

private void merge(int low, int middle, int high) {

    // Copy both parts into the helper array
    for (int i = low; i <= high; i++) {
        helper[i] = numbers[i];
        exchanges++;
    }
    int i = low;
    int j = middle + 1;
    int k = low;
    // Copy the smallest values from either the left or the right side back
    // to the original array
    while (i <= middle && j <= high) {
        if (helper[i] <= helper[j]) {
            numbers[k] = helper[i];
            i++;
            exchanges++;
        } else {
            numbers[k] = helper[j];
            j++;
            exchanges++;
        }
        k++;
        comparisons++;

    }
    // Copy the rest of the left side of the array into the target array
    while (i <= middle) {
        numbers[k] = helper[i];
        exchanges++;
        k++;
        i++;
    }
}   
}
因此,我不知道这是否正确。比较的结果对我来说似乎是正确的,但如果我拿一个20的数组为例,它似乎不再正确了

有谁能帮我一下,告诉我应该把比较和交换增量放在哪里


提前感谢!:)

最简单的方法是创建两个方法,
比较
交换
,并增加其中的计数器。无论实现什么,它都应该只使用这些方法与数组交互


此外,还可以帮助您直观地分析不同的排序算法。

最简单的方法是创建两种方法,
比较
交换
,并增加其中的计数器。无论实现什么,它都应该只使用这些方法与数组交互


此外,它还可以帮助您直观地分析不同的排序算法。

一个包含10个元素的数组很难很好地显示算法性能。为了更好地了解它们在不同输入下的性能,可以尝试比较较大的数组(比如1000个)元素,不同的类型:随机化、已排序、反转等。一个包含10个元素的数组很难很好地指示算法性能。为了更好地了解它们在不同输入下的性能,可以尝试比较较大的数组(比如1000个)元素,它们有不同的风格:随机化、已排序、反转等。
MerSort_Comparisons 22
MerSort_Exchanges 61
MerSort_Operations 83