Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question about linear search and binary search in java Q - Write Java program to compare time consumed by linear search and binary search to

Question about linear search and binary search in java

Q - Write Java program to compare time consumed by linear search and binary search to search for non-exit element in arrays of length 1000, 100000, 500000, 1000000.

Hints:

Use Random class, method nextInt(a.length) to generate random numbers.

Select an element that is greater than a.length.

Use Arrays.sort(a) to sort the array before using binarySearch.

---------------------------------------------------------------------------------

This is the code that i wrote:

package searchingAlgorithms;

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

public class LectureExrcise {

public static void main(String[] args) { int a1[] = new int[1000]; int a2[] = new int [100000]; int a3[] = new int [500000]; int a4[] = new int[1000000]; long startTime = 0; long endTime = 0; long elapsed; Random r = new Random(); for(int i = 0; i

}

-----------------------------------------

but i don't know why when i run it it gave me this: "same excuting time"

image text in transcribed

g E = Console X | x | b 2 = 48 LectureExrcise [Java Application] C:\Program Files\Java\jdk-12.0.2\bin\javaw.exe (14 Feb 2020, 17:44:17) Array with length 1000 Linear search time: 12200 Binary search time: 2100 Array with size 100000 Linear search time: 2100 Binary search time: 2100 Array with size 500000 Linear search time: 2100 Binary search time: 2100 Array with size 1000000 Linear search time: 2100 Binary search time: 2100

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions