-
Notifications
You must be signed in to change notification settings - Fork 97
/
linearSearch.java
49 lines (43 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
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
public class linearSearch
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the array");
int size = input.nextInt(); // size of the array
int[] array = new int[size]; // declared the array
System.out.println("Enter the elements of the array");
for(int i=0; i<size; i++)
{
array[i] = input.nextInt();
}
System.out.println("Enter the element you want to search");
int search = input.nextInt(); // element to search
LinearSearch(array, size, search);
input.close();
}
/**
* this function contains the main logic of linear search
*
* @param array
* @param size
* @param search
*
* @return void
**/
public static void LinearSearch(int[] array, int size, int search)
{
boolean flag = false;
for(int i=0; i<size; i++){
if(search == array[i]){
flag = true;
}
}
if(flag){
System.out.println("Element Found");
}else{
System.out.println("Element not found!!!");
}
}
}