Linear searching is a technique for searching any elements by comparing all the elements of array, until that particular element is found.
In below Java programming tutorial, I have given the source code with output for Linear searching technique.
In below Java programming tutorial, I have given the source code with output for Linear searching technique.
Java Program Source code of Linear Search
import java.util.Scanner; class LinearSearch { public static void main(String args[]) { int c, n, search, array[]; Scanner in = new Scanner(System.in); System.out.printf("Enter number of elements:"); n = in.nextInt(); array = new int[n]; System.out.println("Enter " + n + " integers :"); for (c = 0; c < n; c++) array[c] = in.nextInt(); System.out.printf("Enter value to find:"); search = in.nextInt(); for (c = 0; c < n; c++) { if (array[c] == search) // Searching element is present { System.out.println(search + " is present at location " + (c + 1) + "."); break; } } if (c == n) // Searching element is absent System.out.println(search + " is not present in array."); } }

0 comments:
Post a Comment