The question is
Why is processing a sorted array faster than an unsorted array?
I got this question from mitbbs, it's a interview question.
在下面这个程序中,如果sort后的totalTime 总是比不sort的totalTime 小,
你可否解释是啥原因?
public class Work {
public static void main(String args[]) {
int a[] = new int[1000000000];
//fill a
//sort a
//do not sort a
starttime=System.currentTimeMillis()
for (int i=0;i<a.length;i++) {
process(a[i]);
}
endtime=System.currentTimeMillis()
totalTime = endtime-starttime;
}
process(int element) {
if (element > 256)
System.out.println(element);
else
System.out.println(element);
}
}
你可否解释是啥原因?
public class Work {
public static void main(String args[]) {
int a[] = new int[1000000000];
//fill a
//sort a
//do not sort a
starttime=System.currentTimeMillis()
for (int i=0;i<a.length;i++) {
process(a[i]);
}
endtime=System.currentTimeMillis()
totalTime = endtime-starttime;
}
process(int element) {
if (element > 256)
System.out.println(element);
else
System.out.println(element);
}
}