File size: 639 Bytes
c574d3a |
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 |
public class LinearSearch {
public static void main(String[] args) {
int[] arr = { 10, 20, 30, 40, 50 };
int searchValue = 40;
int result = linearing(arr, searchValue);
if (result == -1) {
System.out.println("Element not in the array");
} else {
System.out.println("Element found at index " + result + " and the search key is " + arr[result]);
}
}
public static int linearing(int[] arr, int x) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
}
|