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
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");
}
}
}
Get Answers For Free
Most questions answered within 1 hours.