Skip to content

Commit 8d7cac0

Browse files
committed
Linear
1 parent e945f16 commit 8d7cac0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.searches;
2+
public class LinearSearchSimple {
3+
public static int LinearSearchSimple(int[] arr, int target) {
4+
for(int i=0; i<arr.length; i++) {
5+
if(arr[i] == target) {
6+
return i;
7+
}
8+
}
9+
return -1;
10+
}
11+
public static void main(String[] args) {
12+
int[] arr = {4, 2, 3, 5, 1};
13+
int target = 5;
14+
int result = LinearSearchSimple(arr, target);
15+
if(result != -1) {
16+
System.out.println("Element found at index: " + result);
17+
} else {
18+
System.out.println("Element not found in the array.");
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)