Question

How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and...

How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and compare with a graph?

I have these methods:

//This one receives one size(n) parameter

   public static ArrayList<Integer> RandomArray(int n){
      
       ArrayList<Integer> randomArray = new ArrayList<Integer>(n);
      
       Random rand = new Random(); //--- random number
       rand.setSeed(System.currentTimeMillis());
      
       for(int i = 0; i < n; i++ ) {
           //loop for creating random numbers until n size
           randomArray.add(i, Math.abs(rand.nextInt() % 256));
       }
       return randomArray;
   }

//I have this one to show the Array

   public static void ShowArray(ArrayList<Integer> array) {
      
       for(int arrayItem : array)
       {
       System.out.println(arrayItem);
       }

   }

//and this ones on my main

ArrayList<Integer> arrayShow = RandomArray(4); // this one to create an ArrayList and using the method RandomArray with the n Size.
       System.out.println(arrayShow);
      
       ArrayList<Integer> sortArrayA = new Sorting().SortX(arrayShow).;
       ArrayList<Integer> sortArrayB = new Sorting().SortY(arrayShow);
       ArrayList<Integer> sortArrayC = new Sorting().SortZ(arrayShow);

Now I have 3 sorting methods in a different file, how can I measure the performance of each algorithm, and compare? Where should I set a timer when I call each sort method, and should I create a loop to run like 5000 times so that I can compare?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java has a API as

System.currentTimeMillis() which gives the current time in milliseconds and return type is long type.

So before calling you method in SortX(), You can do 
long start = System.currentTimeMillis();

and then you call your method as: ArrayList<Integer> sortArrayA = new Sorting().SortX(arrayShow);

Once your method call is done.. You again call long end = System.currentTimeMillis();

Now to get the elapsed time in Sorting, You can do (end - start) and that will be time used in milliseconds.
======

Regarding a loop for 5000 times, You can get the elapsed time in each cycle and then sum them up, and take the average elapsed time.. That way it will be more efficient. Let me know if any more help is needed on this. Thanks!
Add a comment
Know the answer?
Add Answer to:
How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first number current num in main method but when I store after 10 second i'm not geting the update current number. NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the...

  • 1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer...

    1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer >(); ArrayList b = a; a.add(new Integer(4)); b.add(new Integer(5)); a.add(new Integer(6)); a.add(new Integer(7)); System.out.println(b.size()); A)1 B)2 C)3 D)4 E)5 2. Assume the Student and Employee classes each extend the Person class. The Student class overrides the getMoney method in the Person class. Consider the following code:     Person p1, p2, p3;     int m1, m2, m3;     p1 = new Person();     m1 = p1.getMoney();     // assignment 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT