Questions tagged [binary-search]

Binary search is an efficient algorithm for finding an element in a sorted array. The basic idea is to cut the search space in half in each step. The complexity of the algorithm is O(log(n)).

A binary search (or "half-interval search") algorithm finds the position of a specified value (the input "key") within a sorted array. In each step, the algorithm compares the input key value with the key value of the middle element of the array. If the keys match, then a matching element has been found so its index, or position, is returned. Otherwise, if the sought key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input key is greater, on the sub-array to the right. If the remaining array to be searched is reduced to zero, then the key cannot be found in the array and a special "Not found" indication is returned.

A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm.

For pseudo-code and examples in multiple languages, check this Code Codex page.

2936 questions
0
votes
1 answer

Java Heap Space error while writing a program for Square root of an Integer

I am trying to find out the square root of an integer, but in case the integer value is too large for instance - 2147395599. Then the following program gives this exception. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space …
0
votes
1 answer

Adding height of binary tree to insert method

I am creating a program that inserts a character (number/letter) into a binary tree. So far, I'm able to produce an output but it's not what I expected. These are the problems I'm encountering: The insert method is not able to print the correct…
Dae
  • 21
  • 6
0
votes
1 answer

What kind of search algorithm could you use to solve this problem?

Given an array of n integers A[0…n−1], such that ∀i,0≤i
Lmath320
  • 9
  • 1
0
votes
2 answers

ReactJS - I implement Binary Search Function, it works only first time

ReactJS - I implement Binary Search Function, it works only first time but after I change the value in the input box, it always return -1 even it has the value in the Array. Please see the following code: import React, { useState } from…
sirisakc
  • 892
  • 2
  • 15
  • 30
0
votes
0 answers

How to make C++ Binary Search function for a fixed sized array without passing the bounds as arguments

I am a newbie. I wanted to make a binary search function in C++ that didn't take the upper and lower bounds of the array as an arguments. For me, I know the array will be of size 10 no matter what therefore I put r = 9. This code works and took me a…
Caspex
  • 3
  • 2
0
votes
1 answer

Logic Error: Binary search fails with more than two elements in a string array

The objective is to return the index of an element in a string array if present. The method uses a basic binary search using compareTo statements. With more than two elements in a tested array, the method will not detect the present element and…
Matt
  • 7
  • 1
0
votes
2 answers

Max Call Stack exceeded in Binary Search Python

I am trying to implement Binary Search in Python with recursion. I write code above: space = [i for i in range(4096)] # Recursive: def binarySearchRec(arr, low, high, target): if high >= low: mid = (high + low) // 2 if arr[mid]…
0
votes
0 answers

A seemingly simple binary search problem using Python

The problem: There is a list of positive integers where the elements are unique and monotonically increasing. The list is s. For example s=[1,2,3,4,5,6,7,8,9,10]. The length of the list is n. The goal is to find the total number of ordered pairs (L,…
0
votes
1 answer

How to binary search a JSON array of strings?

My class only covered binary searching ints, which is much easier it seems. My assignment is searching Strings. My JSON file is an already sorted list (in alphabetical order) of 20 student names and grades. I'll show one: "Students": [ { "name":…
tristan
  • 5
  • 2
0
votes
1 answer

Binary Search Implementation in C++

I have written a program which should do the following: read product item data from an inventory file and add them in a vector object. The program will allow user to view, search and order the product items. The inventory file should be updated…
jr.çhåvez__07
  • 170
  • 2
  • 9
0
votes
1 answer

Why my mid-1 approach is not working in this binary-search question?

I used mid+1 approach to solve this question and it got solved easily but when wanted to solve the same question my changing the comparison form mid+1 to mid-1 it was not working can you help me out where am I wrong? Problem Link My Initial…
0
votes
1 answer

Start, end and stopping condition of Binary Search code

I hope everyone is doing well. I know there are a lot of questions whose title is very much similar to my question but I have a doubt regarding start and end values :- If I use stopping condition as start<=end then, whether start=0…
0
votes
2 answers

What'll happen if log2(len(list)) is not an integer while doing binary search? How many operations will it have to do?

I'm trying to figure out how many operations binary search would have to do if it were given an array whose's length when "logged" with base 2 would not result in a whole number. I don't really know what to do.
0
votes
0 answers

Binary Search Recursion return nearest value

say I have a sorted integer array: int[] array1 = {1, 3, 5, 7, 9}; int target = 6; // so I am expecting it to return 7 instead of -1 public static int binarySearch(int[] array1, int start, int end, int target) { if (start > end) …
Alex
  • 13
  • 2
0
votes
1 answer

Why is my Binary search stuck in an endless loop?

I just had to pop in here to hopefully get a quick answer to my little problem. I'm trying to create a binary search method but ran into some problems. I created it iteratively, which my IDE(Intellij) apparently didn't like. It had me stuck in a…
user11582989
1 2 3
99
100