多个数组上的排序方法的运行时间
我有各种排序方法,它们都对相同的 100,000 个随机数数组进行排序。
我正在使用以下方法来查找每个数组的运行时间
long insertionStart = System.currentTimeMillis();
arr.Clone(iniArr);
arr.insertionSort();
long insertionFinal = System.currentTimeMillis() - insertionStart;
以及以下随机数数组的运行时间
int maxSize = 100000; // array size
Sortarr arr, iniArr; // reference to array
arr = new Sortarr(maxSize); // create the array
iniArr = new Sortarr(maxSize);
// insert random numbers
Random generator = new Random();
for (int i = 0; i < maxSize; i++) iniArr.insert(generator.nextInt());
如何修改它以便我可以让每个数组对 100 个数组而不是一个数组进行排序,并计算每个数组的时间?例如。运行 1 - 23 毫秒;运行2 - 25ms; ... Run100 - 22ms
编辑: 我还有最后一件事要做。 因此,每次迭代都会以几种方式对数组进行排序,比如插入、合并和快速排序。 因此,插入 = 300 毫秒,合并 = 200 毫秒,快速 = 100 毫秒。我需要在每次迭代中找到排序速度最快的方法。
我知道这是一个简单的最小/最大类型的事情,您在较低的编程课程中会执行一千次。 将每个值放入数组并使用 array.min 调用会更容易吗? (无论它实际上是什么,对于 java 语法来说都是新的..)
I have various sorting methods that are all sorting the same 100,000 random number array.
I'm using the following method to find the runtimes of each
long insertionStart = System.currentTimeMillis();
arr.Clone(iniArr);
arr.insertionSort();
long insertionFinal = System.currentTimeMillis() - insertionStart;
And the following for the random number arrary
int maxSize = 100000; // array size
Sortarr arr, iniArr; // reference to array
arr = new Sortarr(maxSize); // create the array
iniArr = new Sortarr(maxSize);
// insert random numbers
Random generator = new Random();
for (int i = 0; i < maxSize; i++) iniArr.insert(generator.nextInt());
How can I modify this so that I can have each of them sort 100 arrays rather than just one, and count the time of each array? Eg. Run1 - 23ms; Run2 - 25ms; ... Run100 - 22ms
EDIT:
I have one final thing to do.
So each iteration sorts the array a few ways, let's say insertion, merge, and quick sort.
So say insertion = 300ms, merge = 200ms, and quick = 100ms. I need to, for each iteration, find which method sorted the fastest.
I know this is a simple min/max type thing that you do a thousand times in lower programming classes.
Would it be easier to throw each value into an array and use an array.min call? (Whatever it actually is, new to java syntax..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,您似乎正在创建数组,然后使用不同的函数重复排序。
您只需将所有这些放入一个循环中即可。
您可以在打印结果时使用索引
run
。Currently, it looks like you are creating the array and then repeatedly sorting using different functions.
You simply need to put all of that in a loop.
You can use the index
run
while printing out your results.您可能会做类似的事情:
您仍然需要事先进行初始化。我想我不知道为什么在排序之前要克隆数组。可以直接对数组进行排序吗?
You probably would be doing something like:
you'd still need the initialization beforehand. I guess I don't know why the array is cloned before it is sorted. Can you directly sort that array?