Created
June 9, 2018 03:31
-
-
Save thmain/c476e253ed656ec3f35127d14c50b563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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