Ticker

20/recent/ticker-posts

Java Linear search | Java Binary search - Algomentor

Java Linear search and java binary search





Hi, in this article we Learn about 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?

Searching programs in java are very easy and anyone can learn to search easily but if you have knowledge of these topic this is glitter on gold.

What is searching in java?

searching means to find a particular thing in a particular destination. searching is not only a concept of programming but it is the concept of life. We do searching regularly like find where is our phone in our room. But as now we are searching in java so let us consider a programming challenge.

Suppose you are given an array of integer or an 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?

As we already discussed above what we will do in java linear search so now we will directly jump on the program of 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?

Like linear search binary search is another way of the searching program in java this is very fast if we compare with linear search in java. But the array must be in sorted form if we wanted to do a Binary search.

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);
     }
}

In this program, we use recursion in case you don't know what is recursion then recursion means function calling itself again and again with some new conditions.

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.

Thanks for reading that article hope you like our article of java linear search and java binary search here at algomentor we write informative article for you guys so, please follow us on our social handle for more update on an informative blog in c++ and java languages.

Here is the list of the similar article that you might like
If you want to write a post on our website then please check this article on How to do a guest post.

Here are our social link please follow us on our social media Facebook, Instagram.

Post a Comment

0 Comments