-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearching.java
91 lines (85 loc) · 2.14 KB
/
Searching.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
84
85
86
87
88
89
90
91
package matrix;
import java.util.*;
public class Searching {
public static Scanner ss=new Scanner(System.in);
public static Random rr=new Random();
public static void print(int arr[])
{
for(int i=0;i<arr.length-1;i++)
{
System.out.print(arr[i]+",");
}
System.out.print(arr[arr.length-1]);
System.out.println();
}
public static void linearsearch(int arr[])
{
//taking the input to search for the element
System.out.println("Enter the element to search");
int key=ss.nextInt();
int count=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==key)
{
System.out.println("Element is at the position :"+(i+1)); //since for users 0 index is 1
count++;
}
}
if(count==0)
{
System.out.println("Element is not present in the array");
}
}
/*
The time complexity of the above algorithm is O(n).
*/
//-------------------------------------------------------------------------------------------------------------------------------------------
//----------------improve the performance of linear search----------------
/*
Improve Linear Search Worst-Case Complexity
1) if element Found at last ----> O(n) to O(1)
2) if element Not found ----> O(n) to O(n/2)
*/
public static void linearsearch2(int arr[])
{
System.out.println("Enter the element to search");
int key=ss.nextInt();
int left=0;
int right=arr.length-1;
int position=-1;
while(left<=right)
{
if(arr[left]==key)
{
position=left;
System.out.println("Element is found at position :"+(position+1));
break;
}
if(arr[right]==key)
{
position=right;
System.out.println("Element is found at position :"+(position+1));
break;
}
left++;
right--;
}
if(position==-1)
{
System.out.println("Element is not present in the array");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=ss.nextInt();
int arr[]=new int[n];
for(int i=0;i<arr.length;i++)
{
arr[i]=rr.nextInt(100);
}
print(arr);
linearsearch(arr);
linearsearch2(arr);
}
}