Unless this is a homework problem, don't implement your own sorting algorithm.
Use the one already provided by your development environment - it'll be robust, debugged, and almost certainly faster than anything you'll write yourself.
FWIW, the Sort() method on List<T> in .NET uses a QuickSort.
The actual environment (C++ vs .NET vs Java) will have negligable impact, unless you're doing this in an absurdly small amount of memory. Use whatever you have experience with.
public class Main {
private static long test (double[] tosort) {
Date begin = new Date();
Arrays.sort(tosort);
Date end = new Date();
return end.getTime() - begin.getTime();
}
public static void main(String[] args) {
double[] tosort = new double[10700];
for (int jj=0;jj<10;jj++) {
for (int ii=0;ii<tosort.length;ii++) {
tosort[ii] = Math.random();
}
System.out.println("Random data " + test(tosort));
}
for (int jj=0;jj<10;jj++) {
for (int ii=0;ii<tosort.length;ii++) {
tosort[ii] = ii;
}
System.out.println("Presorted data " + test(tosort));
}
for (int jj=0;jj<10;jj++) {
for (int ii=0;ii<tosort.length;ii++) {
tosort[ii] = tosort.length - ii;
}
System.out.println("Inverted data " + test(tosort));
}
}
}
This chunk of code in Java shows how you could determine at least some of the figures you're after :
public class Main {
private static long test (double[] tosort) {
Date begin = new Date();
Arrays.sort(tosort);
Date end = new Date();
return end.getTime() - begin.getTime();
}
public static void main(String[] args) {
double[] tosort = new double[10700];
for (int jj=0;jj<10;jj++) {
for (int ii=0;ii<tosort.length;ii++) {
tosort[ii] = Math.random();
}
System.out.println("Random data " + test(tosort));
}
for (int jj=0;jj<10;jj++) {
for (int ii=0;ii<tosort.length;ii++) {
tosort[ii] = ii;
}
System.out.println("Presorted data " + test(tosort));
}
for (int jj=0;jj<10;jj++) {
for (int ii=0;ii<tosort.length;ii++) {
tosort[ii] = tosort.length - ii;
}
System.out.println("Inverted data " + test(tosort));
}
}
}
Fyi, only my computer each run that code executed stayed below 1 millisecond spent in the sorting routine, I had to increase the data size 100 fold to get some meaningful data.
This piece of code makes entire abstraction of things like the time the comparator code needs (the elements are primitive doubles, comparing other objects will probably take a whole lot more time)
once the just in time compiler has figured out the code, it should become a bit faster as well
you could easily add test runs with alternative sorting algorithms and see how those behave
These figures will vary in function of hardware, input data type, load on your computer, etc, but you can at least get a feeling for what to expect.
You don't need to implement any algorithm (unless this is homework). Every language has its sorting functions, and they are pretty efficient. For example, in C++ you'd use std::sort which on many implementation uses quick sort (and insertion sort if the number of elements is small).
发布评论
评论(3)
除非这是一个家庭作业问题,否则不要实现您自己的排序算法。
使用您的开发环境已经提供的环境 - 它将是健壮的、经过调试的,并且几乎肯定比您自己编写的任何东西都要快。
FWIW,.NET 中
List
上的Sort()
方法使用 QuickSort。实际环境(C++、.NET、Java)的影响可以忽略不计,除非您在内存量极小的情况下执行此操作。使用任何你有经验的东西。
Unless this is a homework problem, don't implement your own sorting algorithm.
Use the one already provided by your development environment - it'll be robust, debugged, and almost certainly faster than anything you'll write yourself.
FWIW, the
Sort()
method onList<T>
in .NET uses a QuickSort.The actual environment (C++ vs .NET vs Java) will have negligable impact, unless you're doing this in an absurdly small amount of memory. Use whatever you have experience with.
Java 中的这段代码显示了如何确定至少一些您想要的数字:
仅供参考,只有我的计算机每次运行执行的代码在排序例程中花费的时间都低于 1 毫秒,我不得不将数据大小增加 100折叠以获得一些有意义的数据。
这些数字会因硬件功能、输入数据类型、计算机负载等而有所不同,但您至少可以了解一下期待。
This chunk of code in Java shows how you could determine at least some of the figures you're after :
Fyi, only my computer each run that code executed stayed below 1 millisecond spent in the sorting routine, I had to increase the data size 100 fold to get some meaningful data.
These figures will vary in function of hardware, input data type, load on your computer, etc, but you can at least get a feeling for what to expect.
您不需要实现任何算法(除非这是家庭作业)。每种语言都有其排序功能,而且它们非常有效。例如,在 C++ 中,您可以使用
std::sort
在许多实现中使用快速排序(如果元素数量很少,则使用插入排序)。You don't need to implement any algorithm (unless this is homework). Every language has its sorting functions, and they are pretty efficient. For example, in C++ you'd use
std::sort
which on many implementation uses quick sort (and insertion sort if the number of elements is small).