简体   繁体   English

为什么这种时间复杂度较小的排序算法需要更长的时间?(已编辑)

[英]Why does this sorting algorithm with smaller time complexity takes longer time?(Edited)

I wrote two simplest sorting algorithms that are almost the same, but the one that has more than half operations to perform takes double the time.我写了两个最简单的排序算法,它们几乎一样,但是一个要执行一半以上的操作需要两倍的时间。 It is beyond my understanding why that is happening.我无法理解为什么会发生这种情况。

Any pointers would be much appreciated since I'm trying to understand the most efficient way to sort, considering different circumstances, a sort as simple as that got me puzzled.任何指针都将不胜感激,因为我试图了解最有效的排序方式,考虑到不同的情况,一种简单的排序让我感到困惑。

public class Sort{

static long timerStart;
static long timerEnd;
static long passedTime;

public static void main(String[] args) {

        double[] arr = createDoubleArray(10000);
        double[] arr2 = Arrays.copyOf(arr,arr.length);

        startTimer();
        sortDoubleArrayFast(arr);
        stopTimer();
        computationTime();

        startTimer();
        sortDoubleArraySlow(arr2);
        stopTimer();
        computationTime();

}
public static void startTimer(){
    timerStart = System.currentTimeMillis();
}
public static void stopTimer(){
    timerEnd = System.currentTimeMillis();
}
public static void computationTime(){
    passedTime = timerEnd-timerStart;
    System.out.println("The operations took: " +passedTime+"ms");
}
public static double[] createDoubleArray(int size){

    double[] output = new double[size];
    for (int i = 0; i < size; i++) {
        output[i] = Math.random()*10000000;
    }

    return output;
}
public static double[] sortDoubleArraySlow(double[] input){
    double tmp;
    int operationNumber = 0;
    for (int j = 0; j < input.length; j++) {
        for (int i = 0; i < input.length; i++) {
            operationNumber++;
            if(input[i]>input[j]){
                tmp = input[j];
                input[j] = input[i];
                input[i] = tmp;
            }
        }
    }
    System.out.println("Operation number in Slow: "+operationNumber);
    return input;
}
public static double[] sortDoubleArrayFast(double[] input){
    double tmp;
    int operationNumber = 0;
    for (int j = 0; j < input.length-1; j++) {
        for (int i = j+1; i < input.length; i++) {
            operationNumber++;
            if(input[i]>input[j]){
                tmp = input[j];
                input[j] = input[i];
                input[i] = tmp;
            }
        }
    }
    System.out.println("Operation number in Fast: "+operationNumber);
    return input;
}

} }

I am surprised that this question got downvoted because as you can see from the comments, the answer is not at all obvious.我很惊讶这个问题被否决了,因为正如您从评论中看到的那样,答案并不明显。 The answer to your question is "relatively" simple - JIT.您的问题的答案“相对”简单 - JIT。 When we have a loop that does exactly the same thing every time, JIT turns it into a piece of native code and executes it directly.当我们有一个每次都做完全相同的事情的循环时,JIT 将其转换为一段本机代码并直接执行。 However, when we change the inner loop every execution (change of start i = j + 1), then JIT does not work, so JVM must reinterpret it, which already causes time overhead.但是,当我们每次执行改变内循环时(改变开始 i = j + 1),那么 JIT 就不起作用了,所以 JVM 必须重新解释它,这已经造成了时间开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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