Skip to content

Instantly share code, notes, and snippets.

@milon
Created January 19, 2018 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milon/f818e073883ee32f00f584ded8f7b11d to your computer and use it in GitHub Desktop.
Save milon/f818e073883ee32f00f584ded8f7b11d to your computer and use it in GitHub Desktop.
Linear Search
//Linear Search
import java.util.Scanner;
public class LinearSearch{
public static int linearSearch(int a[], int key) {
for(int i=0;i<a.length;i++)
if(a[i] == key)
return i+1;
return 0;
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Linear Search");
System.out.print("Enter the number of element in array: ");
int n = input.nextInt();
int arr[] = new int[n];
System.out.print("Enter " + n + " numbers: ");
for(int i=0;i<n;i++)
arr[i] = input.nextInt();
System.out.print("Enter the searching key: ");
int key = input.nextInt();
if(linearSearch(arr, key) != 0)
System.out.println("Key found at " + linearSearch(arr, key) + " position.");
else
System.out.println("Key not found.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment