Questions tagged [binary-search]
The binary-search tag has no usage guidance.
96 questions
4
votes
1
answer
34
views
What if binary search's split was not in the middle, but in a way the left side is twice as long as the right side?
How would the time complexity change if the binary search's split was not in the middle ((left + right)/2), but rather in such a way that the left side is twice as long as the right side?
So we check ...
0
votes
1
answer
36
views
when to search in binary search right, when left?
in the binary search algorithm:
If no items
Return false
If middle item is 50
Return true
Else if 50 < middle item
Search left half
Else if 50 > middle item
Search right half
like can we do the ...
0
votes
1
answer
152
views
Purpose of sizeof(book)/sizeof(string) in lectures' binary sort?
string book[] = {
"Chen",
"Kernighan",
"Leitner",
"Lewis",
"Malan",
"Muller",
"Seltzer",
"Shieber",
"Smith"
}
int main(...
0
votes
1
answer
26
views
Code won't progress unless user inputs a number
I was writing a binary search function before finishing lecture 3 because I wanted to do some practice on my own. I was using scanf, which I had looked up, as I wanted to learn how to code without the ...
0
votes
1
answer
38
views
find problem with my code - CPU overload
I cannot figure out what is wrong with my code.
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
// ensures that the n is non ...
0
votes
1
answer
118
views
pset3 find less comfortable if else expression result unused
I do not understand why I get an errors:
error: expected ';' after expression
else (value > values[l])
and
helpers.c:40:21: error: expression result unused [-Werror,-Wunused-value]
...
0
votes
1
answer
57
views
CS 50 - pset3 find helpers: Why do binary search and selection short not work?
the command line yells at me but I cannot find out why. Any ideas what could be wrong?
Here is my code:
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
include
include "helpers.h"
/...
0
votes
2
answers
246
views
PSET3 find having trouble. Can someone help?
For some reason bubble sort is not working. Can someone help.
binary search bool search (int target, int array[],int length)
{
int end = length-1; int start = 0;
while (array[start] < ...
0
votes
1
answer
72
views
pset3: sort and search work in separate files, but my helpers.c doesn't work
I dealt with sorting in a separate file first, and then I dealt with searching in another file. They seem to work okay or at least worked for the numbers I feed in, but when I put them in helpers and ...
0
votes
0
answers
26
views
For pset3 binary search will this code work?
Here is the code .
bool binary_search (int target, int array[],int length)
{
int end = length-1;
int start = 0;
while ( array[end_num]- array[start_num] > 0)
{ ...
0
votes
1
answer
35
views
do I need recurson for binary search?
Also do I need recursion for any of the pset3 solutions?
If yes any good sources on recursion. There was a quick example in lecture 3. Will that suffice? Preferably a video. I realize I could just ...
0
votes
1
answer
68
views
Find.c - Binary Search probable integer overflow
Here's the gist: https://gist.github.com/Securiteru/a83e4f1a028acd0b1013def793b5dcdc
And the output:
... 60697 61151 61190 61275 61460 61608 61618 61746 61750 61822 62111 62178 62183 62311 62448 ...
0
votes
1
answer
419
views
Pset3, Find, search
In "Find" problem, something seems wrong with my binary search Algorithm, because in check50, helpers.c wouldn't compile, I tried to compile it, then it gave me this weird error:
clang -fsanitize=...
0
votes
2
answers
66
views
pset 3 binary search help
I am working on pset 3 for cs50 on edx, and I'm having quite a bit of trouble with the binary search problem. My program worked fine when I tested it with a linear search (so I think the problem isn'...
0
votes
1
answer
121
views
PSET3 Binary Search Function Infinite Loop
When I try running find.c inputting:
./generate 1000 50 | ./find 127
it should return that the value (127) was found, however when I do it with my code, it seems to create an infinite loop.
Also, ...
0
votes
2
answers
193
views
Pset3 helpers.c
i'm stuck in binary search. Check50 doesn't pass all the checks and it doesn't find the needle, I feel like the algorithm i have implemented is correct but it doesn't work. I've been stuck in this for ...
0
votes
2
answers
163
views
Binary Search for helpers.c
so as the title suggests I am having some trouble with my implementation of binary search. So based on the logic shown in the lecture, I think my implementation is fine, indeed it works for the cases ...
1
vote
2
answers
104
views
Binary Search Pset 3 help
As my question is titled, I would appreciate help with my binary search, shown below. Currently, the search will always return false. I am almost certain that my sorting algorithm is correct, as it ...
0
votes
2
answers
161
views
Segmentation Fault pset3 for Find
I'm getting a segmentation fault error and I can't figure out why.
void sort(int values[], int n)
{
// TODO: implement a sorting algorithm
for (int i = 0; i<n; i++)
{
if ((n!=i+...
0
votes
0
answers
51
views
Result doesn't match expected output
I just finished the helpers.c pset, where you have to implement a counting sort and then a binary search. I havent made it recursive yet but it works in all of my own test cases. I also ran the ./...
0
votes
1
answer
76
views
pset3: search (am I in the right track??)
bool search(int value, int values[], int n)
//search for middle lists
if (value == values [n / 2])
{
return 1;
}
//number greater search left
else if ( value > values[n / 2 ])
{
search(int ...
0
votes
1
answer
109
views
Pset 3 Recursive Binary Search problems - Always returns true
I have tried almost everything and edited my code a hundred times in search of an answer. It seems that whenever I run the program (find.c) the result always resolves to true. Not sure where the ...
2
votes
2
answers
26k
views
Question about binary search. Please help!
https://www.youtube.com/watch?v=5xlIPT1FRcA&t=200s
I am struggling to understand how to implement binary search. According to the video, there are 14 numbers in the array. Then he goes on to ...
0
votes
1
answer
680
views
cs50x pset3 find - Need help with the counting sort
can someone please look at my code and tell me where I went wrong with my counting sort algorithm (Or binary search algorithm, if that's where the problem lies...?). Please and thank you.
Binary ...
0
votes
2
answers
33
views
C: Binary Search searches and finds but incorrect exit code
This code is finding all the numbers correctly. Only problem is it's throwing error of "\ expected an exit code of 1, not 0" when the number is not present. Help would be appreciated.
bool search(int ...
0
votes
1
answer
32
views
Advice re: Binary search - various issues
I need advice as to what to change in this code to make it work. I am new at this so I need specifics, please. Please, no opinions on whether it's trash or what mark you would give if grading it. That ...
1
vote
2
answers
38
views
C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)
This is an attempt at binary search. n is the user input (from main script).
This code compiles but doesn't find the needle in the haystack. I'm a bit lost and would welcome assistance.
bool ...
0
votes
1
answer
59
views
please help me with this binary search
for some reason this binary search code keeps returning true, when providing the haystack from generate.c, even if the "needle" isnt there. when manually inputing the haystack, the code runs perfectly....
0
votes
1
answer
97
views
Pset 3: Find (more comfortable) - child exited with status 255 & exits with wrong code
I ambitiously decided to try the more comfortable version of this task, although I'm very much "less comfortable" with regard to any computer skills... Before I give up, I'd like to ask you what's ...
0
votes
0
answers
218
views
PSET3 \ expected an exit code of 0, not 1 (find.c)
I successfully built my program and went on to check it using check50 only to discover this
:) helpers.c exists
:) helpers.c compiles
:) finds 42 in {42,43,44}
:) finds 42 in {41,42,43}
:) finds 42 ...
0
votes
1
answer
95
views
Pset3 Find - Not sure what is wrong with helpers.c code
I am not sure what is wrong with the code. It seems to work on some of the criteria when using cs50's check50, but does not work on others. There also does not seem to have a pattern about the wrong ...
0
votes
1
answer
39
views
Recursive function problem - return value
I am getting the following error below. It has to do with my binarySearch function.
helpers.c:149:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
However I have a ...
0
votes
1
answer
48
views
Pset3 helpers.c while loop exiting unexpectedly
I am trying to implement a binary search algorithm. However, my while loop isn't working as expected. Having used printf and debug50 with a list of 100 numbers, it seems that the while loop only gets ...
0
votes
1
answer
103
views
Binary Search pset3
bool search(int value, int values[], int n)
{
int found = 0;
int notfound = 0;
int minindex = 0;
int maxindex = n;
int midindex = 0;
do
{
midindex = (minindex + maxindex) / 2;
if (minindex &...
0
votes
1
answer
340
views
PSet 3, binary search implementation
I'm struggling with implementation of binary search in PSet 3, and my code can't quite get past check50.
Here's my code:
bool search(int value, int values[], int n)
{
// TODO: implement a ...
0
votes
1
answer
168
views
helpers.c passes check50 but fails ./generate 1000 50 | ./find 128
check50 gives me all smiles, but when I run the test code ./generate 1000 50 | ./find 127 or 128 they both return "Found needle in haystack". How is that possible? And also, can I be sure my code is ...
0
votes
1
answer
55
views
pset3 binary search , shows errors in check50
i don't understand acc. to the algorithm it should work correctly , but it doesn't
0
votes
1
answer
101
views
pset3 binary search , problems with check50
i have tried a lot but cannot find the meaning of the last two errors of check50 please help
0
votes
1
answer
32
views
pset3: When testing binary search code, how do I print when needle is found?
I would like to test if my binary search function is working correctly. I have created an array that is passed into a working bubble sort function. Printing the sorted array works just fine, all I ...
0
votes
1
answer
81
views
Pset3 recursive binary search function - check50 gives me four :( and I don't know why
I would be really grateful if someone could offer me a hint as to where I'm going wrong with this - I've been stuck for a couple of days now. When I work through the code step by step on paper it ...
0
votes
1
answer
55
views
binary search algorithm
Many sources say that you need to put "int l = n" the element next to last instead of "int l = n - 1"(int f = 0; int l = n - 1; int mid;) in my example.
I am not sure why? Moreover it seems my code ...
0
votes
1
answer
50
views
return -1 does not end function execution
This is my code.
65 int BinarySearch(int key, int array[], int min, int max)
66 {
67 int mid;
68
69 if (max < min)
70 {
71 return -1;
72 }
73 else
74 ...
0
votes
1
answer
167
views
How do I set a pointer to access the left half of an array? [PSET 3 - Binary search]
I'm having trouble accessing only the left half of an array, doing the same for the right half works fine for now, at least I'm not getting any errors. I figured accessing the array using pointers ...
0
votes
1
answer
178
views
Segmentation fault in find.c
I've been working on find.c for a long time now and I've managed to get to the point where it will return the correct output when the needle is in the haystack, but when it isn't I get a segmentation ...
0
votes
2
answers
73
views
find.c compiles but fails check50 - I believe binary search is to blame
Why does my code fail check50?
This is the code of my binary search. I know it's buggy, and I spent the last hours reading answers to similar questions, the information I found doesn't quite cut it ...
0
votes
1
answer
105
views
Check50 is claiming that it's recieving an exit code of 0 rather than 1 (different scenario)
I know this question has been answered before (pset 3 "search": expected an exit code of 0, not 1), but I took a look at it, and it seems that the scenario in which the person asked the ...
0
votes
1
answer
308
views
Stuck in loop on Binary Search
I have some problems with my Binary search and would greatly apprecaite help in this. I've debugged it and it seems that my "mid" variable doesn't change. As such, if "value" doesn't equal value[mid] ...
0
votes
2
answers
67
views
binary search pset 3 pointer to integer error
I am trying my hands on binary search. I get those pointer to integer errors :(
Can sonmeone more experience give me a hand ?
bool search(int value, int values[], int n)
{
int first=0;
int last=n-1;
...
0
votes
1
answer
23
views
Binary search: always false when 2 or 3 numbers entered
My binary search works correctly when you enter 1,4,5,6,7 (etc)... numbers or 'haystacks'. However, when 2 or 3 numbers or 'haystacks' are entered, false is always returned. I have tried everything I ...
0
votes
1
answer
107
views
What's wrong with my recursive binary search function? [duplicate]
I've spent hours on this, have tried multiple configurations based on code I can find online, but am getting no where. Any input would be appreciated. I'm trying to make a recursive, binary search ...