-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Linear_Search.java
44 lines (39 loc) · 921 Bytes
/
Linear_Search.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
38
39
40
41
42
43
44
import java.util.Scanner;
class Linear_Search
{
// Function for linear search
public static int linearSearch(int[] array, int size, int desired)
{
for(int i = 0; i < size; i++)
{
// return position if element is found
if(array[i] == desired)
return i;
}
return -1;
}
// Driver Function
public static void main(String[] args)
{
int num;
Scanner s = new Scanner(System.in);
num = s.nextInt();
int array[] = new int[num];
for (int i = 0; i < num; i++) {
array[i] = s.nextInt();
}
int desired = s.nextInt();
if(linearSearch(array, num, desired) != -1)
System.out.println("Found");
else
System.out.println("Number not found");
}
}
/*
Input :
num = 5
arr = [1,4,5,6,3]
desired = 3
Output :
Number not found
*/