Java program for linear search using the Iterative Approach

By | June 1, 2020

Hello Friends,
In this post, we will talk and learn about How to Write a Java program for linear search using the Iterative Approach.

This program takes input array size & array elements from the user, afterward calling linearSearch(int[] inputArray, int seachKey) method to find search key element in the input array. This algorithm scan search key element in input array if element found then it returns index number else it returns -1

The time complexity of this algorithm is O(n)

 Few Sample output of the above program:

Enter input Array Size:
5
Enter 5 Array Elements:
12
45
67
89
23
Enter Seach Key Element:
67
67 found at index: 2


Enter input Array Size:
6
Enter 6 Array Elements:
12
34
56
78
23
15
Enter Seach Key Element:
100
100 not found in inputArray

You May Also Like:

Which data type would you choose for storing currency values like Trading Price in Java?
How would you print a given currency value for Indian Locale (INR Currency)?
Which classes in java are immutable ?
How would you round a double value to certain decimal Precision and Scale ?
What is BlockingQueue? How can we implement Producer and Consumer problem using Blocking Queue?
How to get number of available processors in Java ?

That’s all about How to Write a Java program for linear search using the Iterative Approach?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *