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
4 answers

Binary search taking more time than linear search

I was recently studying about binary and linear search, and decided to check for real what is the difference in the actual time taken by both of these searching algorithms. Here is my code: #include using namespace std; void…
0
votes
0 answers

Binary search implementation does not work properly in C

#include #include long int binary_search(long int *arr,long int n,long int key){ //Implement binary search long int s = 0; long int e = n - 1; while(s<=e){ long int mid = (s+e)/2; if(arr[mid]…
0
votes
1 answer

Recursive call is not terminating for Binary Search

Could someone tell me what I am doing wrong? My recursive call is not terminating when it sees the "return mid" but it continues to execute the remaining conditions. public class BinSearch { public static int binSearch(int[] a, int key, int…
0
votes
2 answers

Binary Search the hightes value for which a function is false in R

I have an expensiv function that can be called with an positive integer. Starting at zero this function returns false, at a certain value (call this value y) the function will return true and will return also true for all inputvalues higher than…
leo
  • 3,677
  • 7
  • 34
  • 46
0
votes
1 answer

i tried out binary search python problem bt the otp is not crt

pos = -1 def search(list,n): low=0 u = len(list)-1 while low <= u: mid = (low+u)//2 if mid == n: globals()["pos"]=mid return True else: if list[mid] < n: low = mid+1 else: u =…
aadithyajp
  • 11
  • 1
0
votes
1 answer

Binary search algorithm for 2 dimensional approximite data

Here is my specific problem. I need to write an algorithm that: 1) Takes these 2 arrays: a) An array of about 3000 postcodes (or zip codes if you're in the US), with the longitude and latitude of the center point of the areas they cover (that is, 3…
0
votes
1 answer

String vector binary search

I am trying to find a user input word inside a string vector using binary search. But it always returns a positive integer. The vector is sorted and read from a txt file. The file looks like. aah aal aas and so on. int…
0
votes
1 answer

My binary search will find only the element that is located in the middle of the array

So I'm trying to make a binary search to find out by the surname but my code will only find if the element I search is the one located in the middle of the arrays, otherwise it will freeze and crash, does anyone knows what I'm doing wrong? Thank…
Carla
  • 1
0
votes
1 answer

Java ArrayList with custom add function

I know it is possible to override the .add behaviour of ArrayList, but how can it be? Where is it defined in the Java docs? What does it mean to have curly braces {} after the generic definition ? List SortedListOfElements = new…
Nathan B
  • 1,625
  • 1
  • 17
  • 15
0
votes
1 answer

Will std::lower_bound be logarithmic for list<>?

Suppose I have a list and maintaining it in ordered state. Can I isert new values into it with logarithmic complexity with code like this #include #include #include #include using namespace std; ostream&…
Dims
  • 47,675
  • 117
  • 331
  • 600
0
votes
1 answer

Are the space complexity of these 2 Binary Search algorithm the same?

1.recursive binary search (with target position returned) function recursiveBinarySearch(list, target) { const binarySearchHelper = (list, target, left, right) => { let mid = Math.floor((left + right) / 2); //not found if (left >…
0
votes
1 answer

In Python can you pass two arguments to a function where one argument is extrapolated from the other?

I'm writing a recursive binary-search function in Python. My code looks like common examples where the function accepts: (a) a list of numbers; (b) the search term; (c) the length of the list for the initial location of the right pointer; and (d) a…
Ryan
  • 5
  • 1
  • 1
0
votes
2 answers

Why is binary search O(log n), when it runs 4 times?

I saw some videos about O(log n) time complexity, but then I tried a couple of binary search method on the Internet tutorial, and I am more confused now. In computer science, big O notation is used to classify algorithms according to how their…
Racodem
  • 27
  • 5
0
votes
1 answer

Why is binary_search not working as expected?

The question is to find the index of first element in the array that repeats itself atleast once in the array. Input: 7 1 3 4 5 3 7 2 #include #include #include using namespace std; int main() { int n; cin…
0
votes
1 answer

Group integers in a given array where no 2 numbers in the same group has a difference more than k. Find the minimum number of groups

I'm working on an assignment and really struggling coming up with a working solution. I will explain the question first and then walk through how I am thinking about the solution. Question: Input: List nums = [1,5,4,6,8,9,2] k = 3 I have…
adHelius
  • 11
  • 1
  • 3
1 2 3
99
100