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
1
vote
1 answer
Strange behavior with bsearch_index
tmp = [-3,3,5]
p "test: #{tmp.bsearch_index{|j| j == -3}}"
In the above code, I get response as nil. If I compare j against 3, or 5, it works. Why does bsearch_index does not consider very first element?

TorukMakto
- 2,066
- 2
- 24
- 38
1
vote
2 answers
Finding an element in an array of structs using bsearch
So I have an array of structures called conj_gms and what I want to find a specific jogo, given its nome.
Structure:
typedef struct jogo
{
int id;
char equipas[2][1024];
int pont[2];
char nome[MAX_CHARS];
} jogo;
For example i have…

Martim Correia
- 483
- 5
- 16
1
vote
2 answers
doing binary search in an array of strings using bsearch c
So im trying to do a binary search in an array of strings called conj_str the thing is to do that i have to sort it and to that im trying to use qsort the problem is that comparsion function isnt working and its not sorting…

Martim Correia
- 483
- 5
- 16
1
vote
1 answer
How to search an array within a structure using bsearch in c
Please help I don't understand why I can't search an element in an array within a structure to another array within another structure.
Thank you for your help guys would really appreciate it.
#include
#include
#define MAX…

java.begginer
- 59
- 7
1
vote
2 answers
Using Ruby's array.bsearch_index(), is there an elegant way to look for the closest index in array so that array[i] is less than a number n?
I had to use something like
arr = [10, 20, 50, 80, 110]
(arr.bsearch_index{|a| a >= 50} || arr.length) - 1 # => 1
(arr.bsearch_index{|a| a >= 2000} || arr.length) - 1 # => 4
with the return value -1 meaning there is no such index. What if…

nonopolarity
- 146,324
- 131
- 460
- 740
1
vote
2 answers
bsearch and searching range?
bsearch is pretty good for direct search, but what should I use if I need for example search range?
update
for example if i want to find range of values between a and b ( a >= x < b ).
update
range values can be not equal.
so if i have…

excanoe
- 696
- 9
- 16
1
vote
1 answer
C bsearch always returns pointer=NULL
Can someone tell me why in this program bsearch fuction always returns pointer=NULL??
#include
#include
#include
struct data
{
char name[10];
int age;
char eye[15];
};
int komparator (const void* a,…

Piotr Witkoś
- 334
- 3
- 10
1
vote
0 answers
Is there a bsearch that returns the index rather than the value?
Ruby's Array#bsearch assumes a sorted array, takes a block returning true/false, and uses a binary search to find the first element for which the block returns true. For example:
ary = [0, 4, 7, 10, 12]
ary.bsearch {|x| x >= 4 } #=> 4
I want a…

Jonah
- 15,806
- 22
- 87
- 161
1
vote
1 answer
bsearch failed to find string member in an array of structure
Using bsearch in C failed to find a string 'Eva Lam' in an array of structure. This array is sorted in descending order of string members. I checked many times, still don't know where the bug is? BTW, I am using DEV C++ 5.9.4. Please help,…

SJ0407
- 11
- 1
1
vote
5 answers
How can I find the real size of my C++ class?
I'm working on a homework assignment in which I'm required to use char arrays instead of strings and qsort/bsearch. In my call to bsearch below, I know I'm passing the wrong size of Entry, but I'm not sure how to get the real size, and my…

JMP
- 7,734
- 6
- 33
- 34
1
vote
1 answer
bsearch function in c
If I have two functions:
void SortStudents(char *studentList[], size_t studentCount)
{
qsort(studentList, sizeof(studentList)/sizeof(studentList[0]), sizeof(studentList[0]), Compare);
}
int Compare(const void *a, const void *b)
{
return…

Crystal
- 28,460
- 62
- 219
- 393
1
vote
2 answers
Difference between bsearch and find in ruby
I'm trying to match files from a directory to other files in an other directory, and its sub directories using ruby.
I tried to make a small test using this file architecture:
tree .
.
├── src
│ ├── lol
│ │ └── toto
│ └── lolilolpouet
│ …

Antzi
- 12,831
- 7
- 48
- 74
1
vote
2 answers
Is there a way of finding position of element when searching a char array with bsearch()
First time using bsearch() and i was wondering is there a way of finding the position of the element or return the element??
I have bsearch() working and it returns a pointer but from this i am not able to use it to print the element.
void…

Derek O Brien
- 173
- 1
- 3
- 14
1
vote
1 answer
bsearch, array of const chars and pointer arthmetic
I want to use bsearch with array of const chars in order to determine index in this array. Here is the code:
enum _command {dodaj, koniec, usun, wczytaj, wczytajwszystko, zapisz, zapiszwszystko};
const char *_command_s[] = {"dodaj", "koniec",…

ghi
- 697
- 2
- 9
- 20
1
vote
1 answer
Getting "error: invalid initializer" when calling bsearch
I have a struct:
typedef struct DATA {
char *key;
char *parentKey;
char *description;
} DATA;
And an array of instances:
DATA *data_array = NULL; // the global data array
int m_arrayLength = 0; // Keeps track of the number of elements…

qujck
- 14,388
- 4
- 45
- 74