Write a Java Program for Binary Search on Unsorted Array

By | 25.01.2017

Write a Java Program for Binary Search on Unsorted Array


Binary Search

Java Program first ask to the user to enter “how many element he/she want to store in a array”, then ask to enter the array element one by one in any order in which they want”.

After storing the element in the array, following program again ask to the user to enter the element which he/she want to search in the array whether the entered element/number is present in the list or not.

The searching technique used here is binary search which is fast technique :


SOURCE CODE ::

 

import java.util.Scanner;

public class BinarySearch {
    
    //----------------Function to Sort Array-------------------
    
    static int[] sorted(int arr[])
    {
        int l,max,temp;
        l=arr.length;
        for(int i=0;i<l;i++)
        {
            for(int j=i+1;j<l;j++)
            {
                if(arr[i]>arr[j])
                {
                    temp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=temp;
                }
            }  
        }
        return arr;        
    }
    
    //------------function to search key in array-------------
    
    static int binarysearch(int a[],int key)
    {
        int beg,l,end,mid;
        l=a.length;
        beg=0;
        end=l-1;
        mid=(beg+end)/2;
        while(beg<=end)
        {
            if(a[mid]<key)
            {
                beg=mid+1;
                mid=(beg+end)/2;
            }

            else if(a[mid]==key)
            {
                break;
            }
            else
            {
                end=mid-1;
                mid=(beg+end)/2;
            }
        
                if(beg>end)
                {
                    mid=0;
                }
        
        }
        
        return mid;
    }
    

    public static void main(String[] args) {
     
        int i,n,b[],key,c;
        System.out.print("Enter size of array: ");
        Scanner sc = new Scanner(System.in);
        n=sc.nextInt();
        int a[]=new int[n];
        
        //---------Enter values to array-------------
        
        for(i=0;i<n;i++)
        {
           System.out.print(i+1 +" element : "); 
           a[i]=sc.nextInt();
        }
        System.out.print("");
        
        //-----------------Sort the array--------------------
        
        b=sorted(a);
        for(i=0;i<n;i++)
        {
           System.out.println(i+1 +" element : "+b[i]); 
          
        }
        
        //---------------Enter search key u want to find-----------
        
        System.out.println("Enter the number u want to search: ");
        
        key=sc.nextInt();
        
        c=binarysearch(b,key);
        
        if(c==0)
        {
            System.out.println("Sorry,Key doesn't exist in the array");
        }
        else
        {
            System.out.println("Position of element is : "+(c+1));
        }
                
    }
    
}

 

OUTPUT ::

 

Enter size of array: 6
1 element : 3
2 element : 6
3 element : 1
4 element : 8
5 element : 4
6 element : 0


Sorted Array
1 element : 0
2 element : 1
3 element : 3
4 element : 4
5 element : 6
6 element : 8

Enter the number u want to search: 
4
Position of element is : 4

 

5 1 vote
Article Rating
Category: Arrays Programs Java Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sisu

Thank you guys very much