Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.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_Mergesort - Fatal编程技术网

计算合并排序中的交换数(Java)

计算合并排序中的交换数(Java),java,mergesort,Java,Mergesort,我试图找到数组中的元素被交换以对数组进行排序的次数。该程序使用递归和合并排序。经过多次尝试,在我相信的排序发生的地方放置一个计数器,我得到了看起来像是随机生成的数字作为交换数 这是代码。我只想要一个显示正确互换量的数字,而不是随机数 /** * * @author Frank Stalteri * */ public class mergeExample { /** * * @param args */ public static

我试图找到数组中的元素被交换以对数组进行排序的次数。该程序使用递归和合并排序。经过多次尝试,在我相信的排序发生的地方放置一个计数器,我得到了看起来像是随机生成的数字作为交换数

这是代码。我只想要一个显示正确互换量的数字,而不是随机数

    /**
 * 
 * @author Frank Stalteri
 *
 */
public class mergeExample {
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        int [] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
        /**
         * performs merge sort
         */
        mergeSort(list);
        /**
         * prints array to screen
         */
        printArray(list);
    }
    /**
     * 
     * @param list
     */
    public static void mergeSort(int [] list) {
        if (list.length > 1) {
            /**
             * merge first half of array
             */
            int [] firstHalf = new int[list.length / 2];
            System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
            mergeSort(firstHalf);
            /**
             * merge second part of array
             */
            int secondHalfLength = list.length - list.length / 2;
            int [] secondHalf = new int[secondHalfLength];
            System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
            mergeSort(secondHalf);
            /**
             * put the sorted parts together
             */
            merge(firstHalf, secondHalf, list);
        }
    }
    /**
     * 
     * @param list1
     * @param list2
     * @param temp
     * @return 
     */
    public static void merge(int [] list1, int [] list2, int [] temp) {
        int current1 = 0;   //current index in list1
        int current2 = 0;   //current index in list2
        int current3 = 0;   //current index in temp

        while (current1 < list1.length && current2 < list2.length) {
            if (list1[current1] < list2[current2]) {
                temp[current3++] = list1[current1++];
            }
            else {
                temp[current3++] = list2[current2++];
            }
        }
        while (current1 < list1.length) {           
            temp[current3++] = list1[current1++];

        }
        while (current2 < list2.length) {
            temp[current3++] = list2[current2++];
        }
    }
    /**
     * 
     * @param list
     */
    public static void printArray(int [] list) {
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
    }
}

您必须在merge函数之外添加类似count变量的内容,例如在类中添加
static int

公共类合并示例{
静态整数合并计数=0;
// ...
}

每当您在
mergeSort
函数中对代码进行交换时,您必须将
MergeExample.mergeCount
增加1。最后,您将获得在该变量中执行的交换数量

您只打印
列表
,那么什么是打印您的“随机”数字呢?您的代码中没有任何内容。更新itWell打印数字的是我将放在merge函数中的sysout代码。我只是意识到它不在那里,因为我忘了在来这里问问题之前把它取下来了。谢谢你的帮助。看来它奏效了。输出结果改为“1,2,3…19”。@zOrigin_u,这就是它应该的样子,对吗?很高兴我能帮忙。
1
1
1
2
0
0
1
2
5
-2 1 2 2 3 3 5 6 12 14