Skip to content
Advertisement

Compute time to find an element (number) in an array using linear Search

So I got this question in my assignment and I did almost all other than the last part, the code works fine. The question I got is mentioned below.

Write a program to generate any number of random integers in 0 to 100 range. Your program should get the size as a parameter and return the numbers as an array.

Now implement the linear search. Pass the random array of 100 items as the list and “50” as the item to find. Compute the time to find 50 in the array and record the time consumed.

Conduct this test for 10 times

I want to know how to run the array 10 times and compute the time to find 50 separately for each loop.

I have mentioned my code below. My code works completely fine, need to know how to do the last part.

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Array size:t");
        int n = sc.nextInt();
        System.out.println("Element to be found: ");
        int x = sc.nextInt();

        ArrayList list = GenerateRandomIntegers(n, 0, n);

        //printing the array
        StringBuffer sb = new StringBuffer();
        for (Object s : list) {
            sb.append(s);
            sb.append(" ");
        }
        String str = sb.toString();
        System.out.println(str);

        //start computing time
        long startTime = System.nanoTime();

        linearSearch(list, x);

        //end computing time
        long endTime = System.nanoTime();
        long timeElapsed = endTime - startTime;
        System.out.println("Execution time in nanoseconds: " + timeElapsed);
        System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
    }


    public static ArrayList GenerateRandomIntegers(int size, int min, int max) {
        ArrayList numbers = new ArrayList();
        Random rand = new Random();

        while (numbers.size() < size) {
            //getting random numbers within range
            int randomNumber = rand.nextInt((max - min) + 1) + min;
            //check for duplicates
            if (!numbers.contains(randomNumber)) {
                numbers.add(randomNumber);
            }
        }
        return numbers;
    }

    public static void linearSearch(ArrayList arr, int target) {
        for(int i=0;i<arr.size();i++) {
            if(arr.get(i).equals(target)) {
                System.out.println("Item found at = "+(i+1));
            }
        }
    }
}

And this is the output; enter image description here

I hope you got my problem (T^T). Just want to know how to run this 10 times at once (like a loop) and compute time separately for each loop.

Advertisement

Answer

It’s a little ambiguous as it could be asking to simply run the program 10 times; however it is more likely that the program is run once and the search is performed 10 times. It also makes sense that the array is generated each time as well.

You can put the whole thing in a loop…

for(int i = 0; i < 10; i++)
{
    // generate array
    // startTime
    // search
    // endTime
    // output results
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement