Linear search is used to search a key element from multiple elements. Linear search is less used today because it is slower than binary search and hashing.
Algorithm:
- Step 1: Traverse the array
- Step 2: Match the key element with array element
- Step 3: If key element is found, return the index position of the array element
- Step 4: If key element is not found, return -1
Output:
Linear Search in Java (Another way)
You can also use a method where array is not predefined. Here, user has to put the elements as input and select one element to check its location.
Output:
Use image LinearSearchExample
need an explanation for this answer? contact us directly to get an explanation for this answer