Question

In: Computer Science

I want to print out the array as well as its intersection, and also it doesn't...

I want to print out the array as well as its intersection, and also it doesn't show the time elapsed after 100,000

Missing is: 1,000,000 and 10,000,000

import java.util.Random;
import java.util.HashSet;
import java.util.Arrays;

public class array_intersect {

public static void main(String[] args)
{
for(int i=1000; i <= 10000000; i= i*10)
{
Integer[] array1 = generate_random_array(i);
Integer[] array2 = generate_random_array(i);

long startTime = System.nanoTime();
get_intersection(array1, array2);
long stopTime = System.nanoTime();
long elapsedTime = stopTime - startTime;
System.out.println("Time elapsed for dataset of " + i + " points : "+ elapsedTime + " in nanoseconds");

}
}

public static Integer[] generate_random_array(int size)
{
Random rand = new Random();
Integer[] return_array = new Integer[size];
for(int i=0;i< size ; i ++)
{
return_array[i] = rand.nextInt(size);
}
return return_array;
}

public static Integer[] get_intersection(Integer[] array1, Integer[] array2)
{
HashSet<Integer> set = new HashSet<>();
set.addAll(Arrays.asList(array1));
set.retainAll(Arrays.asList(array2));

Integer[] intersection = {};
intersection = set.toArray(intersection);

return intersection;

}


}

Solutions

Expert Solution

Its is printing after 100,000 it just takes more time.

As you can see from the above picture, it takes 939930274600 nanoseconds( ~15 mins) for printing 1000,000 data sets. So if you wait it will print all the results.

For printing, the arrays and insection will mess up your console. I will suggest you avoid it but still, I have provided the code(its highlighted) for that.

Program

import java.util.Random;
import java.util.HashSet;
import java.util.Arrays;

public class Test {

   public static void main(String[] args) {
       for (int i = 1000; i <= 10000000; i = i * 10) {
           Integer[] array1 = generate_random_array(i);
           Integer[] array2 = generate_random_array(i);

           long startTime = System.nanoTime();
           Integer[] intersection=get_intersection(array1, array2);
           long stopTime = System.nanoTime();
           long elapsedTime = stopTime - startTime;
          
          
           System.out.println("Array1: "+Arrays.toString(array1));
           System.out.println("Array2: "+Arrays.toString(array2));
           System.out.println("Intersection: "+Arrays.toString(intersection));

          
           System.out.println("Time elapsed for dataset of " + i + " points : " + elapsedTime + " in nanoseconds");

       }
   }

   public static Integer[] generate_random_array(int size) {
       Random rand = new Random();
       Integer[] return_array = new Integer[size];
       for (int i = 0; i < size; i++) {
           return_array[i] = rand.nextInt(size);
       }
       return return_array;
   }

   public static Integer[] get_intersection(Integer[] array1, Integer[] array2) {
       HashSet<Integer> set = new HashSet<>();
       set.addAll(Arrays.asList(array1));
       set.retainAll(Arrays.asList(array2));

       Integer[] intersection = {};
       intersection = set.toArray(intersection);

       return intersection;

   }

}

If you have any further doubts feel free to comment


Related Solutions

i need this program to also print out the number of combinations #include <stdio.h> #include <string.h>...
i need this program to also print out the number of combinations #include <stdio.h> #include <string.h> #define N 10 void generate(char *array, int n) {    if (n==0)    {        printf("%s\n",array);        return;    }    for (int i = 0; i < n; ++i)    {        // generate all of the permutations that end with the last element        generate(array, n-1);        // swap the element        char temp = array[n-1];...
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
Please explain your answer thoroughly because I want to understand it well and also please include...
Please explain your answer thoroughly because I want to understand it well and also please include a diagram if possible Two objects which have mass: m1 = 10kg and m2 = 20kg. Both of them are moving at the velocity of: v1 = 20^i ms and v2 = 10^j ms and then the two objects collide completely inelastically. In what direction do the two objects go after the collision? After the collision, how much kinetic energy was lost?
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
This is what I have so far. The out print I am looking for is Date...
This is what I have so far. The out print I am looking for is Date is 7/22/2020 New month: 9 New day: 5 New Year: 2020 Date is 9/5/2020 #include    class Date {    public:        Date::Date(int dateMonth, int dateDay, int dateYear)        month{dateMonth}, day{dateDay}, int dateYear}{}               //void displayDate();         //set month        void Date::setMonth(std::int dateMonth){            month = dateMonth;        }        //retrieve month   ...
The following code must be written in Matlab I want to print the following in Matlab...
The following code must be written in Matlab I want to print the following in Matlab (x1,x2, x3) = (0.33333, 0.33333, 0.33333)  . The whole thing should be on the same line. I need to use fprintf and write out the coordinates with 5 decimal places of variable x = (0.33333, 0.33333, 0.33333) Thanks!
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.
  The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {          reverse(a, index); What does it do? Explain why it behaves in this way.                                    There is more than one error in the code. Correct the code so that it will recursively print out...
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
I want to scan in 4 ints and the next 3 lines into an array of...
I want to scan in 4 ints and the next 3 lines into an array of a text file in java. Ex 1 2 3 adacdac acddddd acdadcd 1,2,3 would be stored into seperate int values and the next 3 strings would be stored into an array
anyone can explain why this code in R doesn't work? i also tried to use filter...
anyone can explain why this code in R doesn't work? i also tried to use filter function, no hope either! b <- subset(sub,NEIGHBORHOOD == "HARLEM-CENTRAL") the name of the variable is correct, the condition. is correct too.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT