1

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);
}
}
} 
3
  • Your can, for instance, create an array with all the sizes you want to test, then loop through the array and for each size, create an array with that size and run all the calculations from above Commented Apr 22, 2020 at 22:51
  • how do i have the array get looped if its already being looped ? Commented Apr 22, 2020 at 23:35
  • Before creation. Start the loop before declaring the size. Effectively wrapping all of your application code in one uber-loop Commented Apr 23, 2020 at 0:03

1 Answer 1

1

I would

  • create a new data set for each size
  • and copy for each sort type to ensure fairness
int ds = 100_000;
int [] array = createTestArray(ds);

// Now do your tests, using a copy of the same data
// set for each test.
int [] copy = Arrays.copyOf(array,array.length);
// test1

copy = Arrays.copyOf(array.array.length);
// test2
....

// and so forth.

public static int[] createTestArray(int size) {
     Random rand = new Random();
     int[] array = rand.ints(size).toArray();
     return array;
}

You can iterate the above as you see fit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.