## Java Linear search and java binary search

**java linear search and java binary search**so what are these search techniques. Along with these two algorithms technique. we will implement their program in java language. searching program in java is one of the basic programs of java language and every java learner should master these programs of

**java linear search**and

**java binary search**.

**Prerequisite to learning the searching program in java?**

**What is searching in java?**

**ArrayList**and by mistake, your friend writes 21 instead of 25 and you need to fix that bug. how you will do it. if you know how to do it then very good 😀. But if you don't know how you will do it 😏. then don't worry I hear to help you.

**Here is the solution to your problem**: You need to do searching in a java program and check each element by using a conditional statement and when you find 21 in your program then replace these with 25. and your solution is done and a bug is fixed.

**What are two different methods of searching in java?**

**Linear search in java**: In linear search, we go to each element one by one and check if we get out the desired element our not till the end of array or list.**Binary search in java**: Binary search is a fast way of searching in java. It can do a search in log(n) time. But the main disadvantage of Binary search is the array must be sorted for binary search.

**Write a program for java linear search?**

**Here is the source code of the program of java linear search.**

*import java.lang.*;*

*import java.util.*;*

*import java.io.*;*

*public class HelloWorld*

*{*

*public static void main(String []args)*

*{*

*Scanner sc = new Scanner(System.in);*

*int num,find;*

*System.out.println("Enter a number to element in array ");*

*num = sc.nextInt();*

*int arr[] = new int[num];*

*System.out.println("Enter element of array one by one");*

*for(int i=0;i<num;i++)*

*{*

*arr[i] = sc.nextInt();*

*}*

*System.out.println("Enter a number to find ");*

*find = sc.nextInt();*

*boolean flag=false;*

*for(int i=0;i<num;i++)*

*{*

*if(arr[i]==find)*

*{*

*flag = true;*

*break;*

*}*

*}*

*if(flag==true)*

*{*

*System.out.println("Ya! we find that element");*

*}*

*else*

*{*

*System.out.println("Na! element not in array");*

*}*

*}*

*}*

**Explanation of linear search:**In linear search, we use a boolean flag to find if the element is in the array or not we go to each element one by one and if we find our targetting element we make a flag to true and at last we we check flag value and declare our result.

**Write a program for binary search code java?**

**Here is the source of binary search code java**

*import java.lang.*;*

*import java.util.*;*

*import java.io.*;*

*public class HelloWorld{*

*static int binarySearch(int arr[], int l, int r, int x)*

*{*

*if (r >= l) {*

*int mid = l + (r - l) / 2;*

*if (arr[mid] == x)*

*return mid;*

*if (arr[mid] > x)*

*return binarySearch(arr, l, mid - 1, x);*

*return binarySearch(arr, mid + 1, r, x);*

*}*

*return -1;*

*}*

*public static void main(String []args){*

*Scanner sc = new Scanner(System.in);*

*int num,find;*

*System.out.println("Enter a number to element in array ");*

*num = sc.nextInt();*

*int arr[] = new int[num];*

*System.out.println("Enter element of array one by one");*

*for(int i=0;i<num;i++)*

*{*

*arr[i] = sc.nextInt();*

*}*

*System.out.println("Enter a number to find ");*

*find = sc.nextInt();*

*int result = binarySearch(arr,0,num-1,find);*

*if (result == -1)*

*System.out.println("Element not present");*

*else*

*System.out.println("Element found at index "+result);*

*}*

*}*

**Explanation of Binary search in java**: As you already know from the above content that binary search can only be implemented if the array is sorted. so what we do here is we check the middle element if we find middle element equals to our targeting element then we return the element. and if the middle element is smaller then our targeting element then we check on the right side of array with the same concept. and if the middle element is larger then our targeting element then we check in the left subarray of the main array.

## 0 Comments