Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 9, 2018 03:31
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 thmain/c476e253ed656ec3f35127d14c50b563 to your computer and use it in GitHub Desktop.
Save thmain/c476e253ed656ec3f35127d14c50b563 to your computer and use it in GitHub Desktop.
public class LinearSearch {
static void search(int [] input, int x){
for (int i = 0; i <input.length ; i++) {
if(x==input[i]) {
System.out.println("Element " + x + " is found at index: " + i);
return;
}
}
//if here means x is not found
System.out.println("Element " + x + " is not found in array");
}
public static void main(String[] args) {
int input [] = {20, 30, 40, 10, 5, 2, 60, 73};
int x = 10;
search(input, x);
x = 60;
search(input, x);
x = 9;
search(input, x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment