-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSearch.java
37 lines (33 loc) · 1.04 KB
/
LinearSearch.java
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
import java.time.Instant;
import java.time.Duration;
import java.util.Scanner;
/**
* LinearSearch
*/
public class LinearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = { 1, 2, 4, 6, 11, 25, 34, 67, 69, 84 };
System.out.println("Enter the number to be searched");
int key = sc.nextInt();
sc.close();
Instant start = Instant.now();
int indx = linearSearch(arr, key);
if (indx != -1) {
System.out.println("Element found at index " + indx);
} else {
System.out.println("Element not found");
}
Instant end = Instant.now();
Duration timeElapsed = Duration.between(start, end);
System.out.println("Time taken to execute the program is " + timeElapsed + " milliseconds");
}
static int linearSearch(int arr[], int key) {
for (int i = 0; i <= arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
}