简体   繁体   中英

Java: Is Merge-Sort is O(N^2) or O(N Log(N))

I created my own implementation of merge sort, I tested it that it works. How ever I am not sure if it's O(N Log(N)) as it should be, or it's O(N^2), can you please look at my code and tell?

SortedList

public abstract class SortedList {
    public final ArrayList<Integer> params = new ArrayList<Integer>();

    public void add(int... params) {
        for (int parameter : params) {
            this.params.add(parameter);
        }
    }

    abstract public void sort();

    public void print() {
        for (int parameter : params) {
            System.out.print(parameter + " ");
        }
        System.out.println();
    }
}

MargeSort

public class MargeSort extends SortedList{

    private int buffer[];

    @Override
    public void sort() {
        buffer = new int[params.size()];
        for(int i = 1; i < params.size(); i *= 2){
            sort(i);
        }
    }

    private void sort(int interval) {
        for(int i = 0; i < params.size() - interval; i += interval * 2){
            sort(i, i + interval, interval);
        }
    }

    private void sort(int index1, int index2, int interval) {
        int startingIndex = index1;
        int index1MaxValue = index1 + interval;
        int index2MaxValue = index2 + interval;
        if(index2MaxValue >= params.size()){
            index2MaxValue = params.size();
        }
        int counter = 0;

        for(counter = 0; index1 < index1MaxValue && index2 < index2MaxValue; counter++){
            int param1 = params.get(index1);
            int param2 = params.get(index2);
            if(param1 < param2){
                buffer[counter] = param1;
                index1++;
            }
            else{
                buffer[counter] = param2;
                index2++;
            }
        }
        int index, indexMaxValue;
        if(index1 < index1MaxValue){
            index = index1;
            indexMaxValue = index1MaxValue;
        }
        else{
            index = index2;
            indexMaxValue = index2MaxValue;
        }

        while(index < indexMaxValue){
            int param = params.get(index);
            buffer[counter] = param;
            index++;
            counter++;
        }

        for(int i = 0; i < interval * 2 && i + startingIndex < params.size(); i++){
            params.set(i + startingIndex, buffer[i]);
        }
    }
}

sort(int) is called lg N times, where N = params.size() . [ lg N here and everywhere further means ceil(lg N) ]

Loop in sort(int) loops N / (interval / 2) times, where interval in [1 .. lgN] , calling sort(...) , which takes nr of steps, which is lineary depends on its' interval arg.

So, nr of steps is:

Sigma(k in from 1 to lgN): (N / (interval / 2)) * (C * interval) = C * N/2 * Sigma(1..lgN) 1 = C * N * lgN /2

[ C is constant for accounting of inner sort(...) cost ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM