Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. In Java declare and initialize two arrays (different array names please) with

ID: 3585642 • Letter: 1

Question

1. In Java declare and initialize two arrays (different array names please) with 100,000 integers each. The integers should be consecutive. In one of the arrays, change two of the integers at indexes of your choice.

2. Now compare the two arrays using the code (nested loops) given in the .pdf file. How long did this operation take? Record the time in milliseconds or nanoseconds.

3. Double the size of each array and time the comparison of the two (using the nested loops, of course). How long did this operation take? Is it true that the Number of operations = (size of input)^2 ?

public int countDuplicates(int items1[], int items2[]) {

int count = 0;

for (int i = 0; i < items1.length; i++) {

for (int j = 0; j < items2.length; j++)

{

if (items1[i] == items2[j]) {

count++;

}

}

} return count;

}

Explanation / Answer

1 & 2)

class Solution

{

    public static int countDuplicates(int items1[], int items2[])

    {

        int count = 0;

        for (int i = 0; i < items1.length; i++)

        {

            for (int j = 0; j < items2.length; j++)

            {

                if (items1[i] == items2[j])

                    count++;

                System.out.println("i = " + i + "    j = " + j);

            }

        }

       

        return count;

    }

   

    public static void main(String[] args)

    {

        int[] items1 = new int[100000];

        int[] items2 = new int[100000];

       

        int i;

       

        for(i=0;i<100000;i++)

        {

            items1[i] = i;

            items2[i] = i;

        }

       

        items2[56] = 71;

        items2[94] = 241;

      

        // note the time

        long start_milli = System.currentTimeMillis();

        long start_nano = System.nanoTime();

       

        int count = countDuplicates(items1, items2);

        System.out.println("Count : " + count);

       

        // note the time

        long end_nano = System.nanoTime();

        long end_milli = System.currentTimeMillis();

       

        long time_milli = end_milli - start_milli;

        long time_nano = end_nano - start_nano;

       

        System.out.println("Time Elapsed in milliseconds : " + time_milli);

        System.out.println("Time Elapsed in nanoseconds : " + time_nano);

       

        System.out.println(" With array of double length");

       

        int[] items3 = new int[2 * items1.length];

        int[] items4 = new int[2 * items2.length];

       

        for(i=0;i<2 * items1.length;i++)

        {

            items3[i] = i;

            items4[i] = i;

        }

       

        items4[56] = 71;

        items4[94] = 241;

       

        // note the time

        start_milli = System.currentTimeMillis();

        start_nano = System.nanoTime();

       

        count = countDuplicates(items3, items4);

        System.out.println("Count : " + count);

       

        // note the time

        end_nano = System.nanoTime();

        end_milli = System.currentTimeMillis();

       

        time_milli = end_milli - start_milli;

        time_nano = end_nano - start_nano;

       

        System.out.println("Time Elapsed in milliseconds : " + time_milli);

        System.out.println("Time Elapsed in nanoseconds : " + time_nano);

    }

}

I could not show the output because it takes a lot of time.

3. Yes the number of operations is proportional to (size of input)^2.