-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.java
83 lines (67 loc) · 1.37 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.*;
class Search
{
ArrayList<Integer> array = new ArrayList<Integer>(1);
int item;
/*Search()
{
Scanner scanner = new Scanner(System.in);
}*/
public void getArray()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of elements: ");
while(!scanner.hasNextInt())
{
scanner.next();
System.out.println("Please Enter integer...");
}
int elements = scanner.nextInt();
System.out.println("Enter "+elements+" integers: ");
for(int i =0; i<elements; i++)
{
array.add(i,scanner.nextInt());
}
}
public void linearSearch( int item)
{
int comparisons=0;
for(int i:this.array)
{
comparisons++;
if(item == i)
{
System.out.println("Found...");
break;
}
else if(i == array.size())
{
System.out.println("Not Found....");
break;
}
}
System.out.println("Comparisons: "+comparisons);
}
public void binarySearch(int item)
{
}
public int getItem()
{
int item;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the element want to search: ");
item = scanner.nextInt();
return item;
}
/*public void binarySearch(int array[], int item)
{
}*/
public static void main(String []args)
{
Search aryObj = new Search();
aryObj.getArray();
int item = aryObj.getItem();
aryObj.linearSearch(item);
aryObj.binarySearch(item);
}
}