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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class Example1 { public static void main(String args[]) { int i,key=9; int a[]={1,4,7,9,10}; for (i= 0; i < a.length; i++) { if (a[i] == key) { System.out.println(key+" is present at location "+(i+1)); break; } } if (i == a.length) System.out.println(key + " doesn't exist in array."); } } |
Output:
1 |
9 is present at location 4 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Scanner; class Example2 { public static void main(String args[]) { int i,len, key, array[]; Scanner input = new Scanner(System.in); System.out.println("Enter Array length:"); len = input.nextInt(); array = new int[len]; System.out.println("Enter " + len + " elements"); for (i = 0; i < len; i++) { array[i] = input.nextInt(); } System.out.println("Enter the search key value:"); key = input.nextInt(); for (i = 0; i < len; i++) { if (array[i]== key) { System.out.println(key+" is present at location "+(i+1)); break; } } if (i == len) System.out.println(key + " doesn't exist in array."); } } |
Output1:
1 2 3 4 5 6 7 8 9 10 11 |
Enter Array length: 5 Enter 5 elements 8 3 56 4 8 Enter the search key value: 1 1 doesn't exist in array. |
Output2:
1 2 3 4 5 6 7 8 9 |
Enter Array length: 3 Enter 3 elements 11 21 31 Enter the search key value: 11 11 is present at location 1 |
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”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import java.util.Scanner; class RecursionExample3 { int recursionSearch(int arr[], int start, int last, int x) { if (last < start) return -1; if (arr[start] == x) return start; return recursionSearch(arr, start+1, last, x); } public static void main(String args[]) { RecursionExample3 ref=new RecursionExample3(); int i,len, key, array[]; Scanner input = new Scanner(System.in); System.out.println("Enter Array length:"); len = input.nextInt(); array = new int[len]; System.out.println("Enter " + len + " elements"); for (i = 0; i < len; i++) { array[i] = input.nextInt(); } System.out.println("Enter the search key value:"); key = input.nextInt(); int index=ref.recursionSearch(array,0,len-1,key); if (index!=-1) { System.out.println(key+" is found at location "+(index+1)); } else { System.out.println(key + " doesn't exist in array."); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter Array length: 5 Enter 5 elements 2 4 6 8 9 Enter the search key value: 6 6 is found at location 3 |
If you have any doubts related to Linear search Java program, leave a comment here.
More Java Programs: