문제

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..)

도움이 되었습니까?

해결책

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.

다른 팁

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top