-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinearsearch.java
More file actions
45 lines (36 loc) · 1.29 KB
/
linearsearch.java
File metadata and controls
45 lines (36 loc) · 1.29 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package daaa;
import java.util.Scanner;
public class linearsearch {
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // Return the index if target is found
}
}
return -1; // Return -1 if target is not found
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the size of the array
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
// Input the elements of the array
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
// Input the target element to search
System.out.print("Enter the element to search for: ");
int target = scanner.nextInt();
// Perform linear search
int result = linearSearch(arr, target);
// Output the result
if (result == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index: " + result);
}
scanner.close();
}
}