Binary Search on array

Binary Search on array

What requirement is placed on an array, so that binary search may be used to locate an entry? â?º The array elements must form a heap. â?º The array must have at least 2 entries. â?º The array must be sorted. â?º The arrayâ??s size must be a power of two.

View Answers

August 21, 2012 at 3:01 PM

Here is an example that uses java in built function to search the location of particular array element.

import java.util.Arrays;

public class binary_search {

    public static void main(String[] args) {
           int ar1[]={2,44,5,66,78,90,23,66};
           int p, key=78;
           p=Arrays.binarySearch(ar1, key);
           System.out.println("element found at index "+p);
           Arrays.sort(ar1);
           p=Arrays.binarySearch(ar1, key);
           System.out.println("element found at index "+p);     
    }
}

August 21, 2012 at 3:07 PM

Here is an example that implements binary search function and all the user to add elements to an array in order to search the location of particular array element.

import java.util.*;

public class BinarySearch {
        public static void main(String[] args) {
                int[] intArray = new int[10];
                int searchValue = 0, index;
                System.out.println("Enter 10 numbers");
                Scanner input = new Scanner(System.in);
                for (int i = 0; i < intArray.length; i++) {
                        intArray[i] = input.nextInt();
                }
                System.out.print("Enter a number to search for: ");
                searchValue = input.nextInt();
                index = binarySearch(intArray, searchValue);
                if (index != -1) {
                        System.out.println("Found at index: " + index);
                } else {
                        System.out.println("Not Found");
                }
        }

        static int binarySearch(int[] search, int find) {
                int start, end, midPt;
                start = 0;
                end = search.length - 1;
                while (start <= end) {
                        midPt = (start + end) / 2;
                        if (search[midPt] == find) {
                                return midPt;
                        } else if (search[midPt] < find) {
                                start = midPt + 1;
                        } else {
                                end = midPt - 1;
                        }
                }
                return -1;
        }
}









Related Tutorials/Questions & Answers:
Binary Search on array
Binary Search on array  What requirement is placed on an array, so that binary search may be used to locate an entry? â?º The array elements must form a heap. â?º The array must have at least 2 entries. â?º The array must
JavaScript Array Binary Search
JavaScript Array Binary Search       The JavaScript Binary Search becomes very useful in case of large Arrays.  The Binary Search algorithm is used to  handle
Advertisements
How to using Binary Search Array Java ?
How to using Binary Search Array Java ?  Hi, I am beginners in Java... functions. The problem is that how to use binary search array in Java. Please give any online reference show that i will implement the binary search array in Java
php array binary search
PHP Binary Search Array is used to search the given value in the array. In php there is no function for the binary search like java or other language. User can implement and use the binary search in php as given below
How to create binary search tree using an array?
How to create binary search tree using an array?  hello people, pls guide me on the topic above. i have an string array, i want to make a binary search tree based on data inside this array. the array contains names of people
Java Array Binary Search example
Java Array Binary Search It is a method for searching the array element... example demonstrates how to do a binary search on the Java array object... the binary search algorithm.  It returns the index of the found element
Binary Search in Java
Binary Search in Java In this section, we are going to search an element from an array using Binary Search. The advantage of a binary search over a linear search is astounding for large numbers. It can be done either recursively
Binary Search - Java Beginners
Binary Search  how to use Binary think in java give me the Binary Search programm thx
binary search program
binary search program  write a program to searching a string using binary search
binary search program
binary search program  write a program to searching a string using binary search
binary search program
binary search program  write a program to searching a string using binary search
binary search program
binary search program  write a program to searching a string using binary search
binary search program
binary search program  write a program to searching a string using binary search
Which of the following statements are true with respect to binary search
; Which of the following statements are true with respect to binary search? 1. The array need not be sorted for carrying out binary search 2. Binary search... of the array. 4. The search begins if the search value is equal to the middle
binary search - Java Beginners
binary search  Write a java program to search an array by using recursive binary search.  /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package
Binary Search!!! - Java Beginners
Binary Search!!!  Hi Sir, My question is quite simple. im only... 10 numbers to store in array."); for(int i=0; i<...) { sorted = true;// array potentially sorted for(int i=0; i<
binary search tree
binary search tree  Construct a binary search tree by inserting the following sequence of characters into an empty tree. N O N L I N E A R D A T... for each algorithm. get an item from the user and search the level/levels
binary search tree
binary search tree  Construct a binary search tree by inserting words into an empty tree. "cut your coat according to your cloth" Visit the tree.... get a word from the user and search the level/levels of that word. refer split
Binary Search in Java
Binary Search in Java is used to search an element from an array. Programmers opt for Binary search over linear search when it comes to large numbers. It can... the answer "Not Found".ADS_TO_REPLACE_1 Following is the example of Binary Search
JAVA: Recusrion, Binary Search
JAVA: Recusrion, Binary Search  I want to learn about Binary Search... it using a recursive implementation of Binary Search. For the cases when more than one result can be returned, modify Binary Search to return all the elements
ModuleNotFoundError: No module named 'binary-search'
ModuleNotFoundError: No module named 'binary-search'  Hi, My... 'binary-search' How to remove the ModuleNotFoundError: No module named 'binary-search' error? Thanks   Hi, In your python
Binary Search Tree
Binary Search Tree  Question-1 ) Modify the BinarySearchTree class so that the iterators are fail-fast.Test your class with amain method ? Question-2 ) Modify the BinarySearchTree class so that the BinarySearchTree objects
Binary Search Tree
Binary Search Tree  Question-1 ) Modify the BinarySearchTree class so that the iterators are fail-fast.Test your class with amain method ? Question-2 ) Modify the BinarySearchTree class so that the BinarySearchTree objects
Array search
for the search operation. he value with one or more of the array items. Search begins...Array search  Need a program which performs a searching operation on a one dimensional array called NUM, which is an N-element array stored in a file
binary search tree
binary search tree  how can i make binary search tree? i want write a code that make dictionary with binary search tree data structure.please help me...){ IsEmpty(); currentNode = find(root, number); System.out.println("Search
Binary search tree (insertion) urgent!!
Binary search tree (insertion) urgent!!  Create a program to construct a binary search tree consisting of nodes that each stores an integer.... Assume a binary search tree is constructed from the values 14, 35, 2, 3, 39
ModuleNotFoundError: No module named 'binary-file-search'
ModuleNotFoundError: No module named 'binary-file-search'  Hi, My... named 'binary-file-search' How to remove the ModuleNotFoundError: No module named 'binary-file-search' error? Thanks   Hi, In your
Reading binary file into byte array in Java
Example code of reading binary file into byte array in Java This example shows you how to read a binary file into byte array from Java program. This type of example code is need where you have to read the binary data into byte array
binary search tree from text file
binary search tree from text file  How so I go about constructing a binary search tree from a text file, which has letters and numbers, which must be sorted and printed in ascending order. E.g. Text file contents 3 apples pears
Send me Binary Search - Java Beginners
Send me Binary Search  how to use Binary think in java give me the Binary Search programm thx..  Hi friend, import java.io.*; public class BinarySearchDemo { public static final int NOT_FOUND = -1
PHP Array Search
PHP Array Search In PHP, array_search() function performs a search .... General Format mixed array_search ( mixed $search , array $array [, bool $strict ] ) Parameters $search
PHP Array Sub String Search
PHP Array Search Sub-String In PHP if we need to search a sub-string within... similar to array_search() function. General Desciption of in_array() function... ( mixed $search , array $array [, bool $strict
PHP Array_search functions
Learn PHP Array_search functions There are more then hundreds of functions... in the PHP.  In general array_search function searches array, for the given...;=> 'EShan'); $key = array_search('6872', $array
binary
binary  Hi I want to write a program in pascal that ask a user to input a decimal number and then return its binary equivalent in the minimum number of bits required to repesent the number. Thks
PHP Array Search Key
PHP Array Search Key To check a key of an array exists or not we use array_key... Array Search Key Example 1: <?phpADS_TO_REPLACE_2 $array1=array("... the given key exists within  the array or not. If the key exists in the array
Binary tree
Binary tree  a. Construct a method to implement a binary tree using an array. b. Implement the binary tree to store numbers in sorted order
Search an elements in the array
Search an elements in the array In this section we will discuss about how to check the availability of an element in the array. Array is a collection... is the program to search an element in the array. import java.util.Scanner; public
in_array
in_array  in_array in php
is _array()
is _array()  is_array() in php   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
is _array()
is _array()  is _array()   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
Binary tree
Binary tree  hii, What is binary tree?   hello,ADS_TO_REPLACE_1 A binary tree is a tree in which every node has exactly two links i.e left and right link
binary tree
binary tree  can a binary tree be implemented with out comparing...://www.roseindia.net/java/java-get-example/java-binary-tree-code.shtml http://www.roseindia.net/java/java-get-example/java-binary-tree-insert.shtml
binary tree
binary tree  how to count no. of nodes in a binary tree for mlm if it complet tree or incomplet tree in php using mysql db
octal to binary
octal to binary  i want to know how to convert a octal to binary number   Here is a java example that converts octal to binary. import... binary=Integer.toBinaryString(i); System.out.println(binary
Java read binary file
Java read binary file  I want Java read binary file example code that is easy to read and learn. Thanks   Hi, Please see the code at Reading binary file into byte array in Java. Thanks   Hi, There is many
Array
Array  how can i use elements of an array in a circular form? that is if the searching of the element reach the last elements of the array, then it will start serching from the begining of the array
Array
Array  is it possible to define array like this..? int[] intArray = new int[] {4,5,6,7,8}; Explain...?   Yes, you can. Java Initialize Array
Array
Array  What if i will not declare the limit index of an array, how will I declare an array and store values with it using loop?   Hi Friend... Scanner(System.in); int array[]=new int[5]; System.out.println("Enter
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1

Ads