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
45
votes
1 answer
Is the specification of `bsearch` in C++11 & C++14 defective?
Following on from my answer to this question, in both C++11 and C++14:
[C++11, C++14: 25.5/2]: The contents are the same as the Standard C library header with the following exceptions:
[C++11, C++14: 25.5/3]: The function…

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
19
votes
2 answers
Ruby 2.0.0 Array#bsearch behavior
I noticed that as of Ruby 2.0.0 the array class has a bsearch method that I was testing and I'm not getting the behavior I'd expect. Why does it return a value for 2 and 5 but nil for -1, 1, and 4?
arr_in = [-1, 1, 2, 4, 5]
arr_in.bsearch { |x| x…

jshort
- 1,006
- 8
- 23
10
votes
3 answers
Why does bsearch return a void *?
void * bsearch ( const void * key,
const void * base,
size_t num,
size_t size,
int ( * comparator ) ( const void *, const void * ) );
If I pass in a const void * base, shouldn't…

fredoverflow
- 256,549
- 94
- 388
- 662
10
votes
6 answers
Using bsearch to find index for inserting new element into sorted array
I have a sorted unique array and want to efficiently insert an element into it that is not in the array like this:
a = [1,2,4,5,6]
new_elm = 3
insert_at = a.bsearch_index {|x| x > new_elm } # => 2
a.insert(insert_at, new_elm) # now a =…

Jonah
- 15,806
- 22
- 87
- 161
8
votes
3 answers
Does in_array() use a binary search algorithm?
I have a largish array of string that I want to use as a look-up.
I am using in_array(), but I suspect its doing a simple loop through - does anyone know whether the in_array() algo uses a bsearch algo?

morpheous
- 16,270
- 32
- 89
- 120
6
votes
1 answer
Overload resolution on extern "C" and "C++" version of qsort()/bsearch()
In C++, there are two versions of qsort() provided by the standard library:
extern "C" void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
extern "C++" void qsort(void* base, size_t nmemb, size_t size, int…

goodbyeera
- 3,169
- 2
- 18
- 39
5
votes
2 answers
Trouble using bsearch with an array of strings
I am getting some confusing behaviour trying to use the c builtin bsearch on an array of strings in C. Here is the code. I know you can use the builtin strcmp for searching arrays of strings, but I included myStrCmp for debugging purposes because I…

user2247374
- 53
- 1
- 3
5
votes
5 answers
How to find the insertion point in an array bsearch() works on?
using bsearch() in C (standard library) one can quickly find an entry in a sorted array.
However, how do I calculate where to insert a new entry (using the standard library)?
bsearch() specifically checks whether the found item's key is equal to the…

Danny Milosavljevic
- 634
- 4
- 10
4
votes
1 answer
bsearch() - Finding a string in an array of structs
I have a struct that looks like this:
typedef struct dictionary_t{
char word[30];
int foo;
int bar;
} dictionary_t;
Which forms an ordered array:
dictionary_t dictionary[100];
I would like to search this array for a string using…

Myrmidon
- 65
- 1
- 5
3
votes
3 answers
bsearch and struct (custom type)
I have an array like this:
typedef struct INSTR
{
char* str;
int argc;
} INSTR;
const static INSTR instructions[] = { {"blue",1}, {"green",2} };
then I tried to do a bsearch, but I'm getting Segmentation fault message:
int comp(const void…

Fabricio
- 7,705
- 9
- 52
- 87
3
votes
2 answers
How to use a member function with std::bsearch
I have a C++ class but I'm also using some low level C and need to use the bsearch function. The last argument of bsearch is a comparison function, and I want to implement said function in a way that will allow it to access const private variables…

sparrow2
- 147
- 1
- 11
3
votes
2 answers
bsearch() comparing by name
I have a struct employee comparators and main:
#define MAX_SIZE 20
typedef struct Employee{
char name[MAX_SIZE];
int salary;
int experience;
} employee_t;
void main()
{
int i;
employee_t** employeeArray =…

Coder123
- 784
- 2
- 8
- 29
3
votes
2 answers
C++ function pointer as parameter in Doxygen
I have a situation where I need to document the bsearch() signature in Doxygen. That signature looks like this:
void * __cdecl bsearch (
const void *key,
const void *base,
size_t num,
size_t width,
int(__cdecl *compare)(const…

Mike Morris - MBXSW
- 125
- 1
- 6
3
votes
1 answer
Any way to get bsearch to consistently do equality matching?
As with the sample array from the docs, passing an equality returns inconsistent results:
[0, 4, 7, 10, 12].bsearch{ |x| x == 4} # => nil
[0, 4, 7, 10, 12].bsearch{ |x| x == 7} # => 7
[0, 4, 7, 10, 12].bsearch{ |x| x == 10} # => nil
[0, 4, 7, 10,…

MCB
- 2,021
- 1
- 18
- 32
3
votes
1 answer
Segmentation Fault Using bsearch in C
player_t* getFirstMatch(player_t** sortedPlayers, int playerCount, char* inputString)
{
player_t searchPlayer;
player_t* searchPlayerPointer = &searchPlayer;
searchPlayer.nameLast = inputString;
searchPlayerPointer = (player_t*)…

user2893045
- 59
- 1
- 6