bsearch is a function that performs a binary search. This function may exist in various programming languages, such as C. Use this tag ONLY for existing library functions named "bsearch". Use the [binary-search] tag instead for questions about binary search or self-written implementations.
Questions tagged [bsearch]
87 questions
2
votes
1 answer
different compare funcs for bsearch
Either cmp func seems to work; cant understand how an arg1 of type int* is being resolved in the case of qsort_cmp.
As far as I can figure: int* is passed to qsort_cmp where it is changed to void*, then in return statement cast to struct s*. So far…

dave jordan
- 61
- 6
2
votes
2 answers
qsort and bsearch an array of pointers
I need to sort an array of pointers to struc. In fact, I need to do searching among adresses to see if a given pointer to a struct is present in the array. Unfortunately, I don't have nothing "comparable" inside those structures and so I want to…

Raffo
- 1,642
- 6
- 24
- 41
2
votes
1 answer
Using bsearch() function with array of structs in C
I have an array of structs and I would like to find a key that is an integer in the array of structs.
My structure looks like this:
typedef struct{
int x;
int y;
int area;
int occurence;
}pair;
And I would like to use bsearch() to…

CherryTW
- 33
- 3
2
votes
3 answers
What is Ruby's bsearch find-minimum and find-any behavior?
I am reading the docs for Ruby's bsearch.
It seems when the block returns either true or false, then bsearch works using the "find-minimum" mode. And is there a find-maximum mode?
I don't quite get Ruby's bsearch find-minimum behavior for the 3rd…

nonopolarity
- 146,324
- 131
- 460
- 740
2
votes
2 answers
something similar to stdlib bsearch that returns immediately smaller element
Is there something similar to bsearch built in, that returns the immediately smaller element if the same element is not present and NULL only if the element is already smaller than all other elements. This would require the user to check if the…

0fnt
- 8,211
- 9
- 45
- 62
2
votes
1 answer
bsearch and Visual Studio 2008
I'm using a regexp library that calls the standard function bsearch(). When using VS2008, I must include an extra header for this function to be declared ().
But when I'm linking, it says that bsearch can't be resolved. The MSDN page…

Virus721
- 8,061
- 12
- 67
- 123
2
votes
2 answers
How to use bsearch correctly in c?
I want to search a date (which is a struct) in an array of dates to see if it is in it. This is the first time I am using bsearch and it always returns the same result, 0, whereas it should either return null or a pointer to date found. I am using…

Yiannis
- 929
- 2
- 11
- 14
2
votes
4 answers
Customizing compare in bsearch()
I have an array of addresses that point to integers ( these integers
are sorted in ascending order). They have duplicate values. Ex: 1,
2, 2, 3, 3, 3, 3, 4, 4......
I am trying to get hold of all the values that are greater than a
certain…

Richard Smith
- 29
- 2
2
votes
1 answer
Help with pointers in C using qsort, bsearch
I'm having trouble with some of the pointer/array notation used. I have two lists and am sorting them, and then try to display them. I had 3 comments in my code below as to what the declarations are and why. My code looks like:
int Compare(const…

Crystal
- 28,460
- 62
- 219
- 393
1
vote
2 answers
Failure to work on string comparison using function pointer in C
There are two versions of compare_first_character functions, and they are both passed into another function. However, the first would yield correct matches of words while the second would return nothing found despite there being one.
Version…

Hank Tang
- 51
- 4
1
vote
2 answers
C [-Wincompatible-pointer-types] how to cast
my code: https://godbolt.org/z/de7fbdjh7
code from source: https://stackoverflow.com/a/49072888/15603477
Almost exact the same.
#include
#include
#include
typedef struct
{
int iValue;
int kValue;
char…

asdf
- 13
- 3
1
vote
2 answers
Problem with bsearch() implementation with array of strings
As part of a coding task, I am trying to implement a version of bsearch() and I have experienced an issue with an array of strings.
this is the function I have created:
int binSearch(void *Arr, int Size, int ElemSize,
void *Item, int…

Eli Freid
- 33
- 7
1
vote
0 answers
Refactoring nested each loop in Ruby
I need to do a search comparing two hashs
I use an each to iterate over a hash and b_search to do the binary search:
@player.each do |player|
player_support_search = players_support.bsearch { |player_support| player_support[:score] >=…

Davi Luis
- 396
- 3
- 16
1
vote
1 answer
Need help understanding typecasting const void pointer in C
I have trouble understanding the first line of code inside this implementation of the bsearch function in C. I understand the search algorithm itself and I have played around with this function to get a good grasp of it but I still do not get…

Friedrich Nietzsche
- 65
- 4
1
vote
2 answers
Array#bsearch in Ruby returns nil if the 0 index value is the ONLY value that meets the condition
I provided test cases below, this is just a question to scratch my curiosity.
The ruby documentation for Array#bsearch says:
"This method returns the i-th element. If i is equal to ary.size, it returns nil."
This explanation just isn't clicking in…

Suburban Afro
- 13
- 2