Question

write program to compute intersection of two sorted array of integers and compute the CPU time...

write 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


Java

Homework Answers

Answer #1

CODE

import java.util.Arrays;

import java.util.Random;

public class Main

{

public static void findIntersectionForSortedArrays(int arr1[], int arr2[])

{

int m = arr1.length;

int n = arr2.length;

int i = 0, j = 0;

while (i < m && j < n)

{

if (arr1[i] < arr2[j])

i++;

else if (arr2[j] < arr1[i])

j++;

else

{

System.out.print(arr2[j++]+" ");

i++;

}

}

}

public static int[] getRandomArray(int n)

{

int arr[] = new int[n];

Random random = new Random();

for (int i=0; i<n; i++) {

arr[i] = random.nextInt(100);

}

Arrays.sort(arr);

return arr;

}

public static void main(String args[])

{

for (int i=0; i<3; i++) {

int arr1[] = getRandomArray(1000);

int arr2[] = getRandomArray(1000);

long start = System.nanoTime();

findIntersectionForSortedArrays(arr1, arr2);

long end = System.nanoTime();

System.out.println("\nFor array os size 1000, time taken = " + (end-start) + " nanoseconds");

}

for (int i=0; i<3; i++) {

int arr1[] = getRandomArray(10000);

int arr2[] = getRandomArray(10000);

long start = System.nanoTime();

findIntersectionForSortedArrays(arr1, arr2);

long end = System.nanoTime();

System.out.println("\nFor array os size 10000, time taken = " + (end-start) + " nanoseconds");

}

for (int i=0; i<3; i++) {

int arr1[] = getRandomArray(100000);

int arr2[] = getRandomArray(100000);

long start = System.nanoTime();

findIntersectionForSortedArrays(arr1, arr2);

long end = System.nanoTime();

System.out.println("\nFor array os size 100000, time taken = " + (end-start) + " nanoseconds");

}

for (int i=0; i<3; i++) {

int arr1[] = getRandomArray(1000000);

int arr2[] = getRandomArray(1000000);

long start = System.nanoTime();

findIntersectionForSortedArrays(arr1, arr2);

long end = System.nanoTime();

System.out.println("\nFor array os size 1000000, time taken = " + (end-start) + " nanoseconds");

}

for (int i=0; i<3; i++) {

int arr1[] = getRandomArray(10000000);

int arr2[] = getRandomArray(10000000);

long start = System.nanoTime();

findIntersectionForSortedArrays(arr1, arr2);

long end = System.nanoTime();

System.out.println("\nFor array os size 10000000, time taken = " + (end-start) + " nanoseconds");

}

}

}

Know the answer?
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
Similar Questions
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns...
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns a 2D array of integers of size rows x cols where the value of element [row][col] is row + col. Write a short program to test your method.
Write a program in Java that creates an array of doubles. The array must have size...
Write a program in Java that creates an array of doubles. The array must have size 1000. Fill this array with random numbers between 0 and 1. Then compute and print the total value of all the elements of the array, the mean, the minimum, and the maximum. Filling the array with random numbers must be done by a method that you define and implement. Computing and printing the statistics must be done by another method, which again you must...
C++ Write a program that takes two integer arrays of different sizes from the user (this...
C++ Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10]
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two different classes in the program being made as objects in the main (Bunny class and Dog Class). ask the user "what kind of animal would you like to sort?" user chooses the type of animal (bunny or dog), then you ask the user how big would you like the array of animals to be (maximum array size = 20)? and then you ask what...
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then...
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given N distinct integer points in 1-dimension. That is, all points are integers on the x-axis, and are in an array. This algorithm should go through all pairs (without sorting the elements of the array), one pair at a time, to find the current closest pair. For example, assume we had the following integers: 4, 2, -2, -1 in an array. We would then generate...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT