I am currently timing the different sorting methods when they run 20k random integers, however if i would like to test 1000, 20k, 100k, and 200k
How would i go about doing that ?
and no im not here for someone to just give me the answer, i simply need a bit of guidance.
public static void main(String[] args) {
int n = 10;
int count = 0;
for (int i=0; i < n; i++)
count++;
System.out.println(count);
int size = 20000;
int[] array = new int[size];
populateArrat (array);
long start;
long elapsed;
start = System.currentTimeMillis();
SortingAlgorithms.BubbleSortCS(array.clone());
elapsed = System.currentTimeMillis() - start;
System.out.printf("BubbleSortCS: %,d\n", elapsed);
start = System.currentTimeMillis();
SortingAlgorithms.QuickSort(array.clone(), 0, array.length-1);
elapsed = System.currentTimeMillis() - start;
System.out.printf("Quick Sort: %,d\n", elapsed);
start = System.currentTimeMillis();
SortingAlgorithms.InsertionSort(array.clone());
elapsed = System.currentTimeMillis() - start;
System.out.printf("Insertion Sort: %,d\n", elapsed);
}
public static void populateArrat(int[] array)
{
Random rand = new Random();
for (int i = 0; i < array.length; i++)
array[i] = rand.nextInt(100);
}
}
my Sorting Algorithms class is this
public class SortingAlgorithms {
public static void BubbleSortCS(int[] array)
{
for (int i=0; i <array.length - 1; i++)
{
boolean Swap = false;
for (int j=0; j < array.length - i - 1; j++)
if(array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
Swap = true;
}
if(Swap == false)
break;
}
}
public static void InsertionSort(int[] array)
{
int i, key, j;
for (i = 1; i < array.length; i++)
{
key = array[i];
j = i -1;
while (j >= 0 && array[j] > key)
{
array[j+1] = array[j];
j = j - 1;
}
array[j+1] = key;
}
}
public static int partition( int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
for (int j = low; j<high; j++)
{
if (arr[j] < pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
public static void QuickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
QuickSort(arr, low, pi-1);
QuickSort(arr, pi+1, high);
}
}
}