Latest :

Linear Search In Java Program – 2 Simple Ways | Programs

Java program for linear search – We will discuss the methods on how to carry out the linear search operation in Java.  Compiler has been added so that you can execute the programs by yourself, alongside suitable examples and sample outputs. The methods as mentioned above are:

  • Linear Search – Using Array
  • Linear Search – Using Recursion

Linear Search In Java Program – Standard

 

Output:

Using Array

1) We are searching the key in the array.

2) Read the array length and store the value into the variable len, read the elements using the Scanner class method and store the elements into the array array[].

3) Read the key value and search for that key in the array.

4) Run the for loop

 

  • for i = 0 to  i < length of the array.
  • compare array[i] with the key, If any one of the elements of an array is equal to the key then print the key and position of the key.

Output1:

Output2:

 Using Recursion

1) Read the array length len, store array elements in to the array array[] using Scanner class method.

2) Read the key value and call recursionSearch(array,0,len-1,key) of RecursionExample3 class.

3) RecursionSearch(int arr[], int start, int last, int x)

  • returns -1 value if last<start.
  • Compare the element at the index “start” of the array with the key, if both are equal, returns the index value.
  • if key not equal to the that element call recursionSerach(arr,start+1,last,x) by increasing the start value.

4)  This method returns the index value. If index !=-1, then prints key is found at the location index+1 otherwise, prints “key not available”.

Output:

If you have any doubts related to Linear search Java program, leave a comment here.

More Java Programs:

x

Check Also

Java Inverted Mirrored Right Triangle Star Pattern

Java program to print Inverted mirrored right triangle star pattern program. We have written below ...