了解合并排序

时间:2018-05-05 08:11:19

标签: java sorting

以下是我的合并排序的java实现,我在参加合并排序算法教程后做了。

package com.test.sort;

import java.util.Scanner;


    ////100 80 90 70 60 40 50 30 10 20 //1 3 5 4 2
    public class MergeSortTest {

        private static int[] dataIntAry;

        public static void main(String[] args) {

            System.out.println("Enter data to be sorted : ");
            Scanner scanner = new Scanner(System.in);
            String data = scanner.nextLine();


            String[] dataAry = data.split("\\ ");

            dataIntAry = new int[dataAry.length];

            int cnt = 0;
            for (String dataEntity : dataAry) {
                dataIntAry[cnt] = Integer.parseInt(dataEntity);
                cnt++;
            }
            System.out.println("Array to operate on.");
            print(dataIntAry);
            System.out.println("===================================================================");

            sort(dataIntAry);


            System.out.println("###############################FINAL################################");

            print(dataIntAry);


        }



        private static void sort(int[] array) {

            performMergeSort(0, array.length-1);

        }

        private static void performMergeSort(int lowerIndex, int higherIndex) {
            System.out.println("Operating on array: ");
            print(dataIntAry, lowerIndex, higherIndex);

            //sort only if there is more than one elment in the array
            if(lowerIndex < higherIndex) {
                int middle = lowerIndex + ((higherIndex-lowerIndex)/2);


                performMergeSort(lowerIndex,middle);

                performMergeSort(middle+1, higherIndex);

                merge(lowerIndex,higherIndex);

            }


        }

        private static void merge(int lowerIndex, int higherIndex) {

            System.out.println("Merging array: ");
            print(dataIntAry, lowerIndex, higherIndex);

            for(int i=lowerIndex; i<=higherIndex; i++) {
                for(int j=i+1; j<=higherIndex; j++) {
                    if(dataIntAry[i] > dataIntAry[j]) {
                        int temp = dataIntAry[i];
                        dataIntAry[i] = dataIntAry[j];
                        dataIntAry[j] = temp;
                    }
                }
            }

            System.out.println("After Merge: ");
            print(dataIntAry, lowerIndex, higherIndex);

        }


        private static void print(int[] dataIntAry){
            System.out.println();
            for(int val: dataIntAry){
                System.out.print(val + " ");
            }
            System.out.println();
        }

        private static void print(int[] dataIntAry, int startIndex, int endIndex){
            Systegivingrintln();

            int index = 0;
            for(int val: dataIntAry){
                if(index >= startIndex && index <= endIndex)
                    System.out.print(val + " ");

                index++;
            }
            System.out.println();
        }

    }

此代码对运行时提供的数组进行排序。首先,我怀疑实施是否是核心!如果实施是正确的,我对其性能非常怀疑。 我如何确保实施提供O(n log n)的最差情况? 这里的主要可疑部分是 merge()方法,我在那里进行comapre和swap!。

1 个答案:

答案 0 :(得分:1)

从您的代码中我认为您的合并部分不正确。您应该根据较低,中等和较高的索引进行合并。使用以下方法调用

merge(lowerIndex,higherIndex);

而不是

private void mergeParts(int lowerIndex, int middle, int higherIndex) {

        for (int i = lowerIndex; i <= higherIndex; i++) {
            tempMergArr[i] = array[i];
        }
        int i = lowerIndex;
        int j = middle + 1;
        int k = lowerIndex;
        while (i <= middle && j <= higherIndex) {
            if (tempMergArr[i] <= tempMergArr[j]) {
                array[k] = tempMergArr[i];
                i++;
            } else {
                array[k] = tempMergArr[j];
                j++;
            }
            k++;
        }
        while (i <= middle) {
            array[k] = tempMergArr[i];
            k++;
            i++;
        }

    }

用户以下功能

{{1}}

合并排序的复杂性是O(nlogn)

因为合并排序在每个阶段将数组分成两半,从而为其提供log(n)组件,而另一个N组件来自在每个阶段进行的比较。因此,它的组合几乎成为O(nlog n)。