I'm trying to sort a random array with multiple algorithms and print running times. Each algorithm is supposed to sort the random array. Lets just say I have 3 algorithms; bubble sort, insertion sort and selection sort.
The problem is:
The first algorithm (e.g. bubble sort) in my code sorts the random array and saves it. The other algorithms cant sort it because its already sorted. They just run through sorted arrays print running times. This is not what I want.
What I want is for each algorithm to sort the array. Not only one.
Here is where the problem in my code lies looks like;
// I dont know if i should upload all my code but here is the outline.
for (int i=0; i<noOfTimesToRun; i++) {
// This is supposed to be the random array.
int[] array;
// At this point array is STILL RANDOM.
// Begin algorithm 1.
Start timer;
BubbleSort(array);
End timer;
Elapsed time = End timer - start timer;
//Algorithm 2.
// By now the array is sorted. Its not random. I do not want that.
// So the second algorithm and so forth, runs on sorted arrays.
// === I want to use the SAME RANDOM ARRAY, SO I CAN COMPARE TIMES.
Start timer;
InsertionSort(array);
End timer;
Elapsed time = End timer - start timer;
}
//Print out times here
System.out.println("Bubble Sort Time: " + timeForBubbleSort);

New Topic/Question
Reply



MultiQuote



|