多个数组上的排序方法的运行时间

发布于 2024-10-01 01:55:42 字数 981 浏览 5 评论 0原文

我有各种排序方法,它们都对相同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔女人霸气范 2024-10-08 01:55:42

目前,您似乎正在创建数组,然后使用不同的函数重复排序。

您只需将所有这些放入一个循环中即可。

int maxRuns = 100;

int maxSize = 100000;  // array size

for (int run=0; run<maxRuns; run++) {   
    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());

    long insertionStart = System.currentTimeMillis();
    arr.Clone(iniArr);
    arr.insertionSort();
    long insertionFinal = System.currentTimeMillis() - insertionStart;
    /* <more code goes here> */
}

您可以在打印结果时使用索引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.

int maxRuns = 100;

int maxSize = 100000;  // array size

for (int run=0; run<maxRuns; run++) {   
    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());

    long insertionStart = System.currentTimeMillis();
    arr.Clone(iniArr);
    arr.insertionSort();
    long insertionFinal = System.currentTimeMillis() - insertionStart;
    /* <more code goes here> */
}

You can use the index run while printing out your results.

数理化全能战士 2024-10-08 01:55:42

您可能会做类似的事情:

for (int try = 0; try < 100; try++) {
   iniArr = new Sortarr(maxSize);

   // insert random numbers
   Random generator = new Random();
   for (int i = 0; i < maxSize; i++) iniArr.insert(generator.nextInt());

   long insertionStart = System.currentTimeMillis();
   arr.Clone(iniArr);
   arr.insertionSort();
   long insertionFinal = System.currentTimeMillis() - insertionStart;

   // print out the time, and/or add up the total
}

您仍然需要事先进行初始化。我想我不知道为什么在排序之前要克隆数组。可以直接对数组进行排序吗?

You probably would be doing something like:

for (int try = 0; try < 100; try++) {
   iniArr = new Sortarr(maxSize);

   // insert random numbers
   Random generator = new Random();
   for (int i = 0; i < maxSize; i++) iniArr.insert(generator.nextInt());

   long insertionStart = System.currentTimeMillis();
   arr.Clone(iniArr);
   arr.insertionSort();
   long insertionFinal = System.currentTimeMillis() - insertionStart;

   // print out the time, and/or add up the total
}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文