1
1
import java .util .Scanner ;
2
2
class Search {
3
- public void linearsearch (n , arr )
3
+ public void linearsearch (arr , key )
4
4
{
5
- for
5
+ for (int i =0 ;i <arr .length ;i ++)
6
+ {
7
+ if (arr [i ]==key )
8
+ {
9
+ System .out .println ("Element found at Index" +i );
10
+ return ;
11
+ }
12
+ }
13
+ System .out .println ("Element not found in the array" );
14
+ }
15
+
16
+ public void Binarysearch (arr ,element )
17
+ {
18
+ while (l <=r )
19
+ {
20
+
21
+ }
6
22
}
7
23
}
8
24
class Main {
9
25
public static void main (String [] args ) {
10
26
Scanner sc =new Scanner (System .in );
11
- do {
12
- System .out .println ("Enter the size of the array:" );
13
- n =sc .nextInt ();
14
- int arr [n ];
15
- for (int i =1 ;i ,n ;i ++)
16
- {
17
- arr [i ]=sc .nextInt ();
18
- }
27
+ int n ;
28
+ int [] arr ;
19
29
System .out .println ("MENU DRIVEN ARRAY SORT- SEARCH PROGRAM" );
20
30
System .out .println ("ALGORITHM - Time Complexity" );
21
31
System .out .println ("1.) Linear Search - O(n)" );
@@ -25,15 +35,28 @@ public static void main(String[] args) {
25
35
System .out .println ("5.) Insertion Sort - O(n^2)" );
26
36
System .out .println ("6.) Merge Sort - O(n logn)" );
27
37
System .out .println ("7.) Radix sort - O(nk)" );
28
-
29
- int choice ;
30
-
31
- choice =sc .nextInt ();
38
+ do {
39
+ System .out .println ("Enter the size of the array:" );
40
+ n =sc .nextInt ();
41
+ arr =new int [n ];
42
+ System .out .println ("Enter" +n +"elements in an array:" );
43
+ for (int i =1 ;i ,n ;i ++)
44
+ {
45
+ arr [i ]=sc .nextInt ();
46
+ }
47
+ int choice =sc .nextInt ();
48
+ Search se = new Search ();
32
49
switch (choice )
33
50
{
34
51
case 1 :
35
52
System .out .println ("Searching Algorithm : Linear Search" )
36
-
53
+ System .out .println ("Enter the Key to search:" );
54
+ int key =sc .nextInt ();
55
+ se .linearSearch (arr ,key );
56
+
57
+ case 2 :
58
+ System .out .println ("Searching Algorithm : Binary Search" )
59
+
37
60
38
61
}
39
62
0 commit comments