Write a class LinearSearch
that takes an
int
Array as a parameter when the LinearSearch object is
constructed. The array of values may be sorted or not. The class should
include an int
method find(int n)
that looks
for a value n
in the array, and returns the first location
(index) of that value in the array.
If the value n
isn't found in the array, the method
should return a value of -1
to indicate that the search
failed.
Write a LinearSearchDemo.java
that creates an array of
values and a LinearSearch object, and the demonstrates the use of the
find()
method.
num
elements in an array, about how many
numbers (on average) will the linear search algorithm need to check
before finding the position of a value n
?O(1)
?O(n)
?O(log n)
?O(n log n)
?O(n
2
)
?