0% found this document useful (0 votes)
284 views3 pages

Binary Search of Unsorted Array

This document discusses binary search on an unsorted array in Java. It defines a main method that takes user input to create an integer array, sorts the array, and then performs a binary search to find a target element entered by the user. The binary search logic iterates through half-intervals of the array using floor division to compute midpoints, recursively narrowing the search range until the target is found or the range is exhausted.

Uploaded by

Gobardhan Baral
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
284 views3 pages

Binary Search of Unsorted Array

This document discusses binary search on an unsorted array in Java. It defines a main method that takes user input to create an integer array, sorts the array, and then performs a binary search to find a target element entered by the user. The binary search logic iterates through half-intervals of the array using floor division to compute midpoints, recursively narrowing the search range until the target is found or the range is exhausted.

Uploaded by

Gobardhan Baral
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Wap of Binary Search of unsorted Array?

import java.util.Arrays;
import java.util.Scanner;
public class binary_search {
public static void main(String[] args)
{
int fn, ln, mn,z;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of the array = ");
int size = scan.nextInt();
int arr[] = new int[size];

System.out.println("Length of an arrar is = " +arr.length);

// loop for enter value in an array


for(z=0; z<=arr.length-1; z++)
{
System.out.print("Enter the element " +z+ " in an array = ");
arr[z] = scan.nextInt();
}

// iterate the value form an array


for(int k=0; k<=arr.length-1; k++)
{
int ar = arr[k];
System.out.print(" "+ar+" ");
}

System.out.println();

//sorting an array
for(int s=0; s<(arr.length); s++)
{
for(int n = s+1; n<(arr.length); n++)
{
if(arr[s] > arr[n])
{
int temp = arr[s];
arr[s] = arr[n];
arr[n] = temp;
}
}
}
//print sorted array
System.out.println("sorted array is = ");
for(int sort=0; sort<=(arr.length-1); sort++)
{
int ar = arr[sort];
System.out.print(" "+ar+" ");
}

System.out.println();
//Searching an element in an array using Binary sort
System.out.println("Enter element to be search in array = ");
int search = scan.nextInt();

fn = 0;
ln = size-1;
mn = (fn+ln)/2;

while(fn<=ln)
{
if(arr[mn]<search)
{
fn = mn + 1;
}
else if(arr[mn] == search)
{
System.out.println(search + " element is found at location "
+(mn+1));
break;
}
else
{
ln = mn - 1;
mn = (fn+ln)/2;
}
if(search>arr[size-1])
{
System.out.println(search + " element is not present in the
array");
break;

/*int a = Arrays.binarySearch(arr, search);


System.out.println(a);*/

if(scan!=null)
{
scan.close();
}
if(scan!=null)
{
scan.close();
}
}
}

Output :-
Enter the size of the array = 5
Length of an arrar is = 5
Enter the element 0 in an array = 3
Enter the element 1 in an array = 8
Enter the element 2 in an array = 4
Enter the element 3 in an array = 2
Enter the element 4 in an array = 9
3 8 4 2 9
sorted array is =
2 3 4 8 9
Enter element to be search in array =
4
4 element is found at location 3

You might also like