This document contains a binary search program in Java. It takes user input for the size of an integer array and sorted elements to populate the array. It then takes a search key as input and uses a binary search algorithm to check if the key is present in the array. The program outputs the location if found or nothing if not found.
This document contains a binary search program in Java. It takes user input for the size of an integer array and sorted elements to populate the array. It then takes a search key as input and uses a binary search algorithm to check if the key is present in the array. The program outputs the location if found or nothing if not found.
This document contains a binary search program in Java. It takes user input for the size of an integer array and sorted elements to populate the array. It then takes a search key as input and uses a binary search algorithm to check if the key is present in the array. The program outputs the location if found or nothing if not found.
PROGRAM import java.util.*; public class BinarySearch { public static void main(String[] args) { int[]a=new int[20]; System.out.println("Enter the size of array"); Scanner s=new Scanner(System.in); int n=s.nextInt(); System.out.println("Enter "+n+" sorted elements"); for(int i=0;i<n;i++) a[i]=s.nextInt(); System.out.println("Enter the element to be searched"); int sk=s.nextInt(); int l=0,h = n-1; while(l<h){ int mid=(l+h)/2; if(a[mid]==sk) { System.out.println(sk + " found at location " + (mid + 1)); break; } else if(a[mid]<sk) l=mid+1; else h=mid-1;
} } } OUTPUT Enter the size of array 5 Enter 5 sorted elements 20 30 40 50 60 Enter the element to be searched 50 50 found at location 4
Microsoft 365 Guide to Success: 10 Books in 1 | Kick-start Your Career Learning the Key Information to Master Your Microsoft Office Files to Optimize Your Tasks & Surprise Your Colleagues | Access, Excel, OneDrive, Outlook, PowerPoint, Word, Teams, etc.
Learn Python Programming for Beginners: Best Step-by-Step Guide for Coding with Python, Great for Kids and Adults. Includes Practical Exercises on Data Analysis, Machine Learning and More.
Microsoft PowerPoint Guide for Success: Learn in a Guided Way to Create, Edit & Format Your Presentations Documents to Visual Explain Your Projects & Surprise Your Bosses And Colleagues | Big Four Consulting Firms Method
Microsoft Excel Guide for Success: Transform Your Work with Microsoft Excel, Unleash Formulas, Functions, and Charts to Optimize Tasks and Surpass Expectations [II EDITION]
Microsoft OneNote Guide to Success: Learn In A Guided Way How To Take Digital Notes To Optimize Your Understanding, Tasks, And Projects, Surprising Your Colleagues And Clients
The Advanced Roblox Coding Book: An Unofficial Guide, Updated Edition: Learn How to Script Games, Code Objects and Settings, and Create Your Own World!