小编wil*_*421的帖子

测量的插入排序速度太快

我正在通过使用随机生成的数字为它们提供数组来快速测试Java中的各种排序算法,并且我得到了插入排序的奇怪结果.

我使用System.nanoTime()来测量运行时间,并且插入排序的值低于快速排序和合并排序,即使在排序大型数组时也是如此,这似乎是错误的.我在程序中看到了冒泡排序或选择排序的正常(慢)运行时间,所以我认为我不正确地测量insertSort的速度,但我不确定如何:

public static int[] insertionSort(int[] array) {
    long startTime = System.nanoTime();

    int current;
    int innerIdx;

    int arrayLength = array.length;
    for (int idx = 1; idx < arrayLength; idx++) {
        // iterate through the array, happens just once
        current = array[idx];
        for (innerIdx = idx - 1; current < array[innerIdx] && innerIdx >= 0; innerIdx--) {
            // while current is "traveling" over larger values in array
            array[innerIdx + 1] = array[innerIdx];
            // shift the array elements over by 1 index …
Run Code Online (Sandbox Code Playgroud)

java sorting algorithm insertion-sort

4
推荐指数
1
解决办法
226
查看次数

标签 统计

algorithm ×1

insertion-sort ×1

java ×1

sorting ×1