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 ...
1
vote
2
answers
4k
views
Pset3 Binary Search problems
I have tested the code presented in the picture and find this function always returns false no matter what number you search.There must be a problem in it but I can not see it!! Can anyone help me ? ...
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
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
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
2
answers
367
views
Binary search algorithm only works for smaller arrays
I can't figure out what I'm doing wrong. My algorithm works for smaller arrays, but once the number of values increases, the program stops and I have to hit Ctrl + C. I thought maybe it was an issue ...
0
votes
1
answer
86
views
Recursive Binary Search Exit Code Errors
I wrote the following recursive binary search function. No matter which exit codes I use (e.g., true, false, 1, 0, -1, "Swedish Chef"), I fail at least two of the checks I need to pass. The code ...
0
votes
1
answer
199
views
Trouble with binary search
Using gdb, I found the problem is when
the left_values is not there because if I try print left_values right after declaring it, it tells me No symbol "left_values" in current context.
after ...
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
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
576
views
Pset 3 - Binary Search - clang: error: linker command failed with exit code 1
When I compile, with "make find", the following code:
I get:
So, if I'm interpreting this correctly, there's an undefined reference to search on line 58 of find.c, which is -- if (search(needle, ...
0
votes
1
answer
82
views
binarysearch no errors from compiler, but only spews out gibberish , anyone got an idea?
To make it more readable i thought this time i will just post the link to my helpers.file. The bubblesort worked and the linary search before too.
Maybe i did something wrong in the implementation of ...
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
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
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
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
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
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
1
answer
1k
views
pset3. binary search \ expected an exit code of 0, not 1
First, thanks for helping me on this!
My search function work with the tests:
./generate 1000 50 | ./find 127 -> "Found needle in haystack!"
./generate 1000 50 | ./find 128 -> "Didn't find needle in ...
0
votes
1
answer
785
views
Pset3 Segmentation Fault
Where is the segmentation fault occurring in this program?
Thanks!
/**
* helpers.c
*
* Computer Science 50
* Problem Set 3
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#...
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
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 ...
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 ...
2
votes
2
answers
132
views
how effective would it have been to use an array for the dictionary?
I just completed pset5 using a trie, but I'm curious about other methods.
How effective would it have been to load the dictionary into an array and then use a binary search function to locate words ...
0
votes
1
answer
895
views
Find.c (Binary Search)
I am having some serious issues figuring out what is wrong with my code. I have run this through on paper with the simplest of numbers and cant seem to understand why it fails. I could use some ...
0
votes
1
answer
325
views
Need help with recursive binary search
Here is my helpers.c file, I have been scouring the message boards and nothing is making sense. I use the pset3 input of ./generate 1000 50 | ./find 127. I get a Didn't find needle in haystack ...
0
votes
2
answers
816
views
What's wrong with this binary search implemented using recursion?
This is pset3 and I've tried making it work for numerous hours. It always returns false whether the 'needle' is found in the 'haystack' of arrays or not.
When typing this in the command window: ./...
0
votes
1
answer
585
views
Pset3 Search/Find problems
I have no idea what the problems are with this code, so any help would be appreciated. The code compiles, but the output is skewed. It only sorts after a certain amount of characters, and then sorts ...
0
votes
1
answer
915
views
Implementation of Search Function
I am having trouble understanding why the bool search function is working the way it does.
I had gone through the Section for Week 3. While I understand the logic of binary search, I don't understand ...
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
411
views
Bug in my Pset3 Binary Search helpers.c
When I run check50
check50 2015.fall.pset3.find helpers.c
This is the terminal result
:) helpers.c exists
:) helpers.c compiles
:( finds 42 in {42,43,44}
\ expected an exit code of 0, not 1
:) ...
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
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] ...
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
1
answer
319
views
Pset3: Helpers.c Binary Search Fails on checks that have 42 entered last
For some reason my implementation of binary search for Pset3 fails to work correctly whenever the last number added to the "haystack" is 42. During use of GDB, I found that this is due to the part of ...
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
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
503
views
Pset3 - Binary Search : Control may reach end of non void function
I have written my binary search code, but every time i try to compile I get the following error
jharvard@appliance (~/Dropbox/pset3/find): make helpers
clang -ggdb3 -O0 -std=c99 -Wall -Werror ...
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
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
270
views
Having trouble with pset3 helpers.c binary search
bool search(int value, int values[], int n)
{
int midpoint = n / 2;
int startpoint = 0;
int endpoint = n - 1;
// if the midpoint doesn't equal the vales we're looking for, repeat this ...
0
votes
1
answer
143
views
Odd bug for Find
I've been working of the find (problem set 3) for 2 weeks and I'm just stuck.
I already have the design etc worked out but there is a bug that I seem to be missing.
So the problem is that when I run ...
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
1
answer
525
views
pset 3 "search": expected an exit code of 0, not 1
I'm at the very end of the "find, sort, search" section of pset3, but when using check50, I get "expected an exit code of 0, not 1". This only happens on the checks where check50 is looking for a ...
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
645
views
Slice a list for recursive function in pset3 search
I'm using a recursive function for pset3's binary search. How do you call the same function again giving half the list as an argument?
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
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 ...
0
votes
3
answers
625
views
pset 3 binary search
I know the logic behind binary search is simple but my code isn't compiling. I went along the lines of week 3's section. I am still getting compilation errors.
int beginning = 0;
int ending = n - 1;
...
1
vote
1
answer
172
views
pset3 - recursive binary search not compiling
I wrote a recursive function for binary search in pset" and I keep getting the same error from clang: "control may reach end on non-void function" it is driving me crazy... some help would be very ...