-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchAlgorithmPractice.java
More file actions
60 lines (55 loc) · 1.8 KB
/
SearchAlgorithmPractice.java
File metadata and controls
60 lines (55 loc) · 1.8 KB
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
import java.net.Inet4Address;
import java.util.PriorityQueue;
public class SearchAlgorithmPractice {
public static int linearSearch(int[] arr, int ele){
for(int i=0;i<arr.length;i++){
if(arr[i]==ele){
return i;
}
}
return -1;
}
//without recursion
public static int binarySearch(int[] arr, int ele){
int left = 0;
int right = arr.length-1;
int middle;
while(left<=right){
middle = left+right/2;
if(arr[middle]==ele){
return middle;
}else if(arr[middle]>ele){
right = middle -1;
}else{
left = middle + 1;
}
}
return -1;
}
public static int binarySearchWithRecursion(int[] arr, int ele, int left, int right){
if(right<left){
return -1;
}
int middle = left+right/2;
if(arr[middle]==ele){
return middle;
}else if(arr[middle]>ele){
right = middle - 1;
return binarySearchWithRecursion(arr,ele,left,right);
}else{
left = middle + 1;
return binarySearchWithRecursion(arr,ele,left,right);
}
}
public static void main(String[] args) {
int[] arr = {4,2,5,1,7,3};
System.out.println("Linear Search >>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println(linearSearch(arr,3));
System.out.println("Binary Search >>>>>>>>>>>>>>>>>>>>>>>>");
SortingAlgorithmsPractice.mergeSort(arr);
System.out.println(binarySearch(arr,9));
System.out.println(binarySearch(arr,3));
System.out.println(binarySearchWithRecursion(arr,9, 0,arr.length-1));
System.out.println(binarySearchWithRecursion(arr,3, 0,arr.length-1));
}
}