Question

In: Computer Science

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. 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

NO HASH SET

Solutions

Expert Solution

For the above program, the following are the things we need to have into consideration.

  1. We need to run this for 5 different sets of integers and 3 times each.
  2. We need to use random generator in order to find the array elements.

Following is the code in java with the output having the time taken.

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

public class Intersection {
   public static int intersection(int a[], int b[])
   {
       int i = 0, j = 0;
       while(i<a.length && j<b.length) {
           if(a[i] == b[j]) {
               return a[i];
           }else if(a[i] > b[j]) {
               j++;
           }else {
               i++;
           }
       }
       return -1;
   }
   public static void main(String args[]) {
       Random rand = new Random();
       int integers = 1000;
       // Runing it for 1000, 10000, 100000, 1000000, 10000000
       for(int i = 0;i<5;i++) {
           // running three times same number
           for(int j = 0;j<3;j++) {
               // start time
               long start_time = System.currentTimeMillis();
           //declaring the integers
               int a[] = new int[integers];
               int b[] = new int[integers];
               // putting the integers in the array
               for(int x = 0;x<integers;x++) {
                   a[x] = rand.nextInt(integers) + 1;
                   b[x] = rand.nextInt(integers) + 1;
               }
               // Sort the array
               Arrays.sort(a);
               Arrays.sort(b);
              
               // Finding the intersection
               intersection(a, b);;
               // end_time
               long end_time = System.currentTimeMillis();
               // Printing the time
               System.out.println("Integers: " + integers + ", Iteration: " + (j+1) + " Time Taken: " + (end_time - start_time));
           }
           integers = integers*10;
       }
      
   }
}

Following is the snippet of the above program and time taken is in milli seconds:

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like if you think effort deserves a like.
Thanks


Related Solutions

Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
Write an application that uses multithreading to compute the sum of the integers in an array...
Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT