My goal is to use various sorting methods to run 500 iterations of two random(each time) arrays, calculate the runtime and store it into an array. When all 500 iterations are done, I want to average the times and print out the average for each array size at the end, not each time the loop runs. I am currently getting the following error: "Index 500 out of bounds for length 500" I am also getting the same average for each time the loop runs. How can I run an unsorted array each time through my loop? How can I store the value? How can I do it again for the other array size?
My example of bubble Sort is as follows:
public static void main(String[] args) {
{
int[] smallUnsortedArray = createArrayWithRandomInts(99);
bubbleSort(smallUnsortedArray);
int[] largeUnsortedArray = createArrayWithRandomInts(10000);
bubbleSort(largeUnsortedArray);
}
}
/**
* Sorting an array of ints in ascending order using bubbleSort
* Best-Case Complexity: O(n), Average Complexity: O(n^2), Worst-Case Complexity: O(n^2)
* O(n) is achieved in Best-Case (already sorted array) using the alreadySorted flag
* @param array
* @return
*/
static int[] bubbleSort(int[] array)
{
int bubbleSortTime[] = new int[500];
int iterations = 0;
while(iterations < 500) {
int temp;
boolean alreadySorted = true;
long start = System.nanoTime();
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length - 1; j++)
{
if (array[j] > array[j + 1])
{
alreadySorted = false;
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
if (alreadySorted == true)
{
break;
}
;
long end = System.nanoTime();
long bubbleSortRunTime = end - start;
bubbleSortTime[i] = (int) bubbleSortRunTime;
i++;
}
// getting array length
int length = bubbleSortTime.length;
// default sum value.
int sum = 0;
// sum of all values in array using for loop
for (int i = 0; i < bubbleSortTime.length; i++) {
sum += bubbleSortTime[i];
}
double average = sum / bubbleSortTime.length;
System.out.println("Average of bubble sort : "+average + " nano seconds.");
}
iterations++;
}
return array;
}
static int[] createArrayWithRandomInts(int size)
{
int[] array = new int[size];
for (int i = 0; i < size; i++)
{
array[i] = (int) (Math.random() * Math.random() * 100000);
}
return array;
}
}
I had tried it this way(original):
static int[] bubbleSort(int[] array)
{
int bubbleSortTime[] = new int[500];
int x, y;
for (x = 0; x< 500; x++) {
int temp;
boolean alreadySorted = true;
long start = System.nanoTime();
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length - 1; j++)
{
if (array[j] > array[j + 1])
{
alreadySorted = false;
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
if (alreadySorted == true)
{
break;
}
}
long end = System.nanoTime();
long bubbleSortRunTime = end - start;
bubbleSortTime[x] = (int) bubbleSortRunTime;
x++;
}
// getting array length
int length = bubbleSortTime.length;
// default sum value.
int sum = 0;
// sum of all values in array using for loop
for (int i = 0; i < bubbleSortTime.length; i++) {
sum += bubbleSortTime[i];
}
double average = sum / length;
System.out.println("Average of bubble sort : "+average + " nano seconds.");
}
return array;
}
I'm not sure where my logic is breaking down. I think my current build is better than the previous, but I didn't get the out of bounds error with the original.