算法比QuickSort快

时间:2016-10-07 13:17:06

标签: algorithm sorting big-o quicksort mergesort

我是初学者编码器,我想出了一种排序算法(DexSort),它通常比标准Quicksort快得多。这假设集/数组中所有可能值的数量小于N ^ 2,其中N是我尝试排序的项目数。我正在尝试找到一种优化它的方法,因此它不一定要依赖于所有可能的值,而只需要依赖于相关值的子集。

例如....说我有一个随机数组,其中array.length = 1000万。当所有可能值的总数小于N ^ 2(即10 ^ 7 * 10 ^ 7 = 10 ^ 14)时,我的算法仅比Quicksort(平均)快。现在,假设在数组中可以找到10 ^ 14个实际值。在这一瞬间,我的算法将在大约O(10 ^ 14)运行。任何人都可以想到一种可以减少这种情况的方法吗?

这是我在Java中的代码:

package sort;

import java.util.*;

public class DexSort {

public static Comparable[] dexSort(Comparable[] c, int max){
    //The variable int max is the maximum number of possible values
    //E.g.  If you are trying to sort 7-digit phone numbers, max = Math.pow(10,8) - 1, or 10^8 - 1

    int size = c.length;
    Comparable[] sorted = new Comparable[size];
    int[] array = new int[max+1];

    for (int i = 0; i < size; i++){
        int val = (int) c[i];
        int count = array[val];
        count++;
        array[val] = count;
    }

    int next = 0;
    while (next < size){
        for (int i = 0; i <= max; i++){
            int count = array[i];
            if (count > 0){
                for (int j = 0; j < count; j++){
                    sorted[next] = i;
                    next++;
                }
            }
        }
    }
    return sorted;
}

public static void main(String[] args){

    Random r = new Random(7);

    for (double n = 4; n < 8; n++){

        double size = Math.pow(10, n);
        System.out.println("---------------------------------------------");
        System.out.println("Filling array size: 10^" + n);
        System.out.println("---------------------------------------------\n");

        Comparable[] array = fillArray((int)size, r);   //Create array of random numbers of specified size
        System.out.println("Array filled");         //Tests different array sizes by incrementing a power of 10
        System.out.println("---------------------------------------------\n");

        double max = size;  //Arbitrarily set the maximum value possible as the array size
                            //Runtime will depend heavily on max if max>>>> size (See dexSort method)
                            //Overall, runtime will be O(max) when max >>>>> size

        double t0 = System.nanoTime();
        array = dexSort(array, (int) max);
        double tF = System.nanoTime();

        double nanoSecs = tF - t0;
        double secs = nanoSecs/Math.pow(10, 9);

        System.out.println("DEX sort complete");
        System.out.println("It took " + String.format("%.3f", secs) + " seconds to sort an array of size 10^" + n);

        //printArray(array);  //Uncomment this line to print sorted array to console
        System.out.println();
        System.out.println("---------------------------------------------");
        System.out.println("---------------------------------------------\n\n");
    }
}

public static Comparable[] fillArray(int size, Random r){
    Comparable[] c = new Comparable[size];
    for (int i = 0; i < size; i++){
        /*if ((i+1)%10000000 == 0){
            System.out.println(((i+1)/1000000) + " million filled");
        }*/
        c[i] = r.nextInt(size)+1;
    }
    return c;
}


public static void printArray(Comparable[] c){
    for (int i = 0; i < c.length; i++){
        if (i%10 == 0){
            System.out.println();
        }
        System.out.print(c[i] + "\t");
    }
}

}

0 个答案:

没有答案