排序数组的最快方法,无需覆盖它

时间:2015-05-21 11:29:54

标签: java arrays sorting

我想在Java中对int[] array进行排序,但是将排序后的数组存储为新数组而不是覆盖它。

最明显的方法是创建数组的副本,然后对新数组进行排序,如下所示:

int[] a2 = new int[a.length];

for (int i = 0; i < this.length; i++) {
    a2[i] = a[i];
}

Arrays.sort(a2);

然而,有更快的方法吗?我们可以同时排序&#39;当我们将旧数组的元素复制到新数组中时?

4 个答案:

答案 0 :(得分:11)

您可以使用

int[] a2 = IntStream.of(a).sorted().toArray();

但我怀疑它比

更快
int[] a2 = Arrays.copyOf(a, a.length);
Arrays.sort(a2);

无论它的复杂程度如何,所以不要期望不仅仅是一个恒定的因子加速。

答案 1 :(得分:1)

在迭代旧数组时对新数组进行排序不会更快。为此,您可以使用System.arraycopy()而不是创建自己的排序或复制功能。

  

将指定源数组中的数组从指定位置开始复制到目标数组的指定位置。

示例:

class StackDemo {
    public static void main(String[] arg) throws java.io.IOException {
        Stack s1 = new Stack();
        char ch1;
        System.out
                .println("would you like to push data into stack??? Enter y for yes. Enter n for no");
        ch1 = (char) System.in.read();
        Scanner scan = new Scanner(System.in);
        if (ch1 == 'y') {
            for (int i = 0; i < 10; i++) {
                int input;
                System.out.println("Enter an integer to push to the stack");
                input = scan.nextInt();
                s1.push(input);
            }
        } else if (ch1 == 'n') {
        } else {
            System.out.println("push - Enter a valid input");
        }
        System.out.println("Stack Values: " + s1);
    }
}

更新:只要您要求更快排序数组的方式,正如您所看到的那样,对于复制或排序是否存在长时间的讨论苍蝇将是最好的选择。为此你可以尝试做一些基准测试。但如果您使用副本对其进行排序,您会在此处找到all depends of the size and elements of the array

答案 2 :(得分:0)

如果您倾向于使用TreeSet,则以下代码可用于此目的:

TreeSet<Integer> copied = new TreeSet<>();
for (int i = 0; i < array.length; i++) {
    copied.add(i);
}

我试图测试差异,它实际上取决于数据的大小以及数组中的数据。

但是,我无法评论这种方法的确定性如何,但我肯定会进行一些实验并在此发布我的发现。

<强>更新

我使用以下代码测试TreeSet

的性能
import java.util.Arrays;
import java.util.Random;
import java.util.TreeSet;

class TestArrayCopy {

    public static void main(String[] arg) throws java.io.IOException {
        for (int i = 1; i <= 10; i++) {
            int arr[] = randomArray();
            System.out.println("Array Size: " + arr.length + ". Results for case #" + i);
            System.out.println("Using Array Copy:");
            copyAndSort(arr);
            System.out.println("Using Tree Set:");
            useTreeSet(arr);
            System.out.println("----------------------------");
        }
    }

    public static void copyAndSort(int array[]) {
        long start = System.nanoTime();
        for (int j = 0; j < 100; j++) {
            int copied[] = Arrays.copyOf(array, array.length);
            Arrays.sort(copied);
        }
        long end = System.nanoTime();
        System.out.println(end - start);
    }

    public static void useTreeSet(int array[]) {
        long start = System.nanoTime();
        for (int j = 0; j < 100; j++) {
            TreeSet<Integer> copied = new TreeSet<>();
            for (int i = 0; i < array.length; i++) {
                copied.add(i);
            }
        }
        long end = System.nanoTime();
        System.out.println(end - start);
    }

    public static int[] randomArray() {
        Random random = new Random();
        int len = 100000 + random.nextInt(1000000);
        int arr[] = new int[len];
        for (int i = 0; i < len; i++) {
            arr[i] = random.nextInt(1000000);
        }
        return arr;
    }

}

使用Java 8在Core-i7 64位系统上实现了以下结果:

  

阵列大小:616568。案例#1的结果使用阵列复制:
  7692926921
使用树集:
16336650396
  ----------------------------
数组大小:390270。案例#2的结果使用数组复制:
4441066232
使用树集:
9306742954
  ----------------------------
数组大小:658410。案例#3的结果使用数组复制:
8534144532
使用树集:
17721764026
  ----------------------------
数组大小:1021396。案例#4的结果使用数组复制:
13573512678
使用树集:
31341152398
  ----------------------------
数组大小:1034872。案例#5的结果使用数组复制:
13298690836
使用树集:
30950793986
  ----------------------------
数组大小:466014。案例#6的结果使用阵列复制:
5501196272
使用树集:
11704757934
  ----------------------------
数组大小:190231。案例#7的结果使用数组复制:
1662270714
使用树集:
4465174267
  ----------------------------
数组大小:681150。案例#8的结果使用数组复制:
8262756493
使用树集:
19079310588
  ----------------------------
数组大小:627257。案例#9的结果使用数组复制:
6725984653
使用树集:
14468898852
  ----------------------------
数组大小:397189。案例#10的结果使用数组复制:
3122214311
使用树集:
7356182877
  ----------------------------

从这些结果可以看出,TreeSet比排序重复数组慢很多。对我好运动。

答案 3 :(得分:0)

int [] a2 = IntStream.of(a).sorted()。toArray();