forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearSearch.java
37 lines (36 loc) · 1.13 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.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[5];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter the value to be searched: ");
int x = sc.nextInt();
sc.close();
long start = System.nanoTime();
int i = linear(a, x);
long end = System.nanoTime();
long execution = end - start;
double seconds = (double) execution/ 1000000000.0;
if (i == -1){
System.out.println("Not Found");
}
else {
System.out.println("Found at index: " + i);
}
System.out.println("Execution time: " + seconds + " seconds");
}
public static int linear(int[] a, int x){
int index = 0;
while (index< a.length){
if (a[index] == x){
return index;
}
index++;
}
return -1;
}
}