Question

Write a program to compute intersection of two sorted array of integers and compute the CPU...

Write a program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator using HASH SET.

Test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers

DONT FORGET CPU TIME FOR EACH ONE

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

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
* This program is written in java language which generates two unsigned integer arrays
* and then generate intersection of two arrays
*
*/
public class GenerateArrayCPUTimeAndIntersection {

   public static void main(String[] args) {
       final int iteration = 3; //for each dataset size 3 times intersection iteration
       Integer[] array1 = null; // stores first array generated by dataset size
       Integer[] array2 = null; // stores second array generated by dataset size
       long time1, time2;
       long arrayTime1, arrayTime2;

       List<Integer> dataSetSizeList = new ArrayList<>(5);
       dataSetSizeList.add(1000);
       dataSetSizeList.add(10000);
       dataSetSizeList.add(100000);
       dataSetSizeList.add(1000000);
       dataSetSizeList.add(10000000);

       for (Integer dataSetSize : dataSetSizeList) {
           for (int i = 1; i <= iteration; i++) {
               System.out.println("*******************************************");
               System.out.println("Iteration number: " + i);
               System.out.println("Dataset size: " + dataSetSize);
               time1 = System.currentTimeMillis();
               array1 = getUnsingedNumbers(dataSetSize);
               time2 = System.currentTimeMillis();
               arrayTime1 = time2 - time1;
               time1 = System.currentTimeMillis();
               array2 = getUnsingedNumbers(dataSetSize);
               time2 = System.currentTimeMillis();
               arrayTime2 = time2 - time1;
               System.out.println("First Array Generation Time: " + arrayTime1 + " millisecond(s)");
               System.out.println("Second Array Generation Time: " + arrayTime2 + " millisecond(s)");
               List<Integer> intersectValues = generateIntersection(array1, array2);
               System.out.println(intersectValues);
           }
       }
       System.out.println("*******************************************");
   }

   /**Generates intersection of array1 and array2
   * @param array1
   * @param array2
   * @return
   */
   public static List<Integer> generateIntersection(Integer[] array1, Integer[] array2)
   {
       HashSet<Integer> set = new HashSet<>();
       List<Integer> intersectValues = new LinkedList<Integer>();
       for (int i = 0; i < array1.length; i++)
           set.add(array1[i]);

       for (int i = 0; i < array2.length; i++) {
           if (set.contains(array2[i])) {
               intersectValues.add(array2[i]);
           }
       }
       return intersectValues;
   }


   /**This method generates unsigned numbers array based on passed size
   * @param arraySize
   * @return
   */
   public static Integer[] getUnsingedNumbers(int arraySize) {
       HashSet<Integer> set = new HashSet<Integer>();
       Random random = new Random();
       while(set.size() <= arraySize){
           int thisOne = random.nextInt((Integer.MAX_VALUE - 1) + 1) + 1;
           set.add(thisOne);
       }
       Integer[] array = new Integer[set.size()];
       set.toArray(array);
       return array;
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a program to compute intersection of two sorted array of integers and compute the CPU...
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
  • Write an application that uses parallelism to compute the sum of the integers in an array...

    Write an application that uses parallelism to compute the sum of the integers in an array of size 100000. You can populate the array with random numbers and your application should display the sum.

  • Write a computer program that prompts the user for one number, n for the number of...

    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 different 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. In the body of the loop, Create an array of n random integers (Make sure...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • Description: Write a complete C++ program that will do the following: 1. Declare an array of...

    Description: Write a complete C++ program that will do the following: 1. Declare an array of 30 elements 2. Set the array to random numbers between 0 to 100 inclusive. Don't seed the random number generator. 3. Output the array. 4. Sort the array in ascending order using the selection sort. 5. Output the sorted array. Required IO: Array by F. Last Original: 1 5 ... 2 10 + Sorted: 1 2 ... 5 10 ( +

  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection...

    (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection Sort • Rotates the sorted array around a random point, e.g., 1 2 3 4 5 à 3 4 5 1 2 is obtained by rotation around 3. • Searches for a key point in the rotated array in O(logn) time, where the rotation point is known in advance. • Repeats above by first finding the unknown rotation point via sequential search and binary...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

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