forked from fineanmol/Hacktoberfest2026
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
26 lines (24 loc) · 717 Bytes
/
Copy pathLinearSearch.java
File metadata and controls
26 lines (24 loc) · 717 Bytes
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
26
// Code for linearly searching x in arr[].
//If found returns the location.
//If not found returns -1.
public class LinearSearch {
public static int search(int arr[], int x) {
int size = arr.length;
for (int i = 0; i < size; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
// Driver code
public static void main(String args[]) {
int arr[] = {11, 41, 47, 108, 490};
int x = 14;
// Function call
int result = search(arr, x);
if (result == -1)
System.out.print("Element is not present in array");
else
System.out.print("Element is present at index " + result);
}
}