Skip to main content
4 votes
Accepted

Question about binary search. Please help!

Even or odd, it doesn't really matter. The point is that you are dividing the list approximately in half, give or take. If the split doesn't occur exactly at the middle (odd number of elements), it ...
Cliff B's user avatar
  • 69k
3 votes
Accepted

How do I set a pointer to access the left half of an array? [PSET 3 - Binary search]

For a binary search, you keep only one range, not two. That one could be specified by either a pointer to the start of the range and a length, or a left and right index limiting the range (the version ...
Blauelf's user avatar
  • 21k
3 votes
Accepted

Need help with recursive binary search

When you call a recursive function, you actually need to deal with the return value of that function. You are currently just calling BinarySearch(key, array, min, midpoint-1) for example and then ...
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

how effective would it have been to use an array for the dictionary?

suppose we are to implement speller using an array instead of a hash-table or a trie. a main problem that we have is that we don't know the number of words in the dictionary in advance (yes, we know ...
kzidane's user avatar
  • 17.7k
2 votes
Accepted

find.c compiles but fails check50 - I believe binary search is to blame

The reason that the other questions aren't helping is that you've come up with a problem that appears to be unique. Take a look at the following code, particularly the test condition: if (values[...
Cliff B's user avatar
  • 69k
2 votes
Accepted

Binary Search pset3

Q1: Remember this: COMPILERS ARE DUMB! While it may be logically impossible to not execute a return, compilers aren't capable of interpreting all of the complex logic possibilities to know. So, they ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

return -1 does not end function execution

It does return, but you have to understand where it returns to. When a return executes, it returns control to the function that called it. In this case, since BinarySearch() recursively calls itself, ...
Cliff B's user avatar
  • 69k
2 votes

C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)

Actually what you had written is not a binary search as in each move its not changing the size of array to half of original size. By the way mistake in your code is in line no. 19 and 30 as you are ...
Rahul's user avatar
  • 252
1 vote
Accepted

C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)

When you write int min; int max; you're creating two int variables, but their value is undefined, it's the value that happened to be at that place in memory when they were created. You don't assign ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Recursive function problem - return value

Currently, if binarySearch does not score a direct hit (values[midpoint] == value), it does not return anything. In those cases, however, you should return the result of the recursive call, which you ...
Blauelf's user avatar
  • 21k
1 vote

PSET3 find having trouble. Can someone help?

The array[start] < array[end] is the wrong condition. You want to keep looping as long as the search interval has elements, so use start <= end instead (<= as you chose both start and ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

pset3: sort and search work in separate files, but my helpers.c doesn't work

Your search function never returns true. Ever. if (value == values[m]) { return 0; } return 0 is the same as return false. It should be return 1 or return true.
robert_x44's user avatar
1 vote

Find.c - Binary Search probable integer overflow

Your values are like +---+---+---+---+ |min|max|mid|<>=| +---+---+---+---+ | 0|999|499| < | | 0|498|249| < | | 0|248|124| < | | 0|123| 61| < | | 0| 60| 30| < | | 0| 29| 14| ...
Blauelf's user avatar
  • 21k
1 vote

pset 3 binary search help

Your while loop runs only when n > 1. You are not considering the case when there is just one element left for consideration and that is in fact the element to be searched for. So your code would ...
N. Tiwari's user avatar
1 vote
Accepted

pset 3 binary search help

No looks like the problem is in sort. Linear sort is working indeed because it doesn't depend on correct sort, but binary does. And the problem is that you sort in reverse order, while doing binary ...
obesman's user avatar
  • 1,139
1 vote
Accepted

PSET3 Binary Search Function Infinite Loop

Seems you just misprinted. Change end = middle + 1 to start = middle + 1 and it will work just fine.
obesman's user avatar
  • 1,139
1 vote

Binary Search for helpers.c

Your code will keep on running as long as n>0, and since n is static, it keeps on going even if the value is not in the array. A better way would be to keep track of how many times you've looked ...
ronga's user avatar
  • 1,415
1 vote
Accepted

Binary Search for helpers.c

"killed by server" usually indicates an infinite loop, which is exactly what is happening here. Take a good look at your search function and consider what happens when a number is not in the list. ...
Cliff B's user avatar
  • 69k
1 vote

Binary Search Pset 3 help

The only thing wrong in your searching algorithm that i'm seeing is that you have your if statement followed by a second if statement. I believe that that should be an else if.
Davis's user avatar
  • 144
1 vote

Segmentation Fault pset3 for Find

Much better now. Your Binsearch works fine, but sort doesn't. Segmentation fault comes from here values[i+1] = values[max] As max is a value in haystack, not an index of haystack[]; Correct it and ...
obesman's user avatar
  • 1,139
1 vote
Accepted

Segmentation Fault pset3 for Find

You have two problems here: First is that your sort function does not sort. Just look here, for example: if ((n<i+1) && (n>1)) This condition will NEVER be true as you loop for i < ...
obesman's user avatar
  • 1,139
1 vote
Accepted

cs50x pset3 find - Need help with the counting sort

One problem I see with your sort function is that you've made a third array (sortedArray[]) and filled it with sorted numbers from values[], but then you didn't do anything else with it. Your function ...
ArtemPetrov's user avatar
1 vote
Accepted

C: Binary Search searches and finds but incorrect exit code

Appropriate while loop would be while (max >= min), looping as long as the search interval has elements. If values[mid] > value, it means the value (if present) would be on the left, so set max ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Advice re: Binary search - various issues

Here is Zamyla's pseudocode for search while (length of list > 0) look at middle of list if number found, return true else if number higher, search left else if number lower, ...
Yuu's user avatar
  • 26
1 vote
Accepted

Pset3 recursive binary search function - check50 gives me four :( and I don't know why

Based on the comments in your code and without doing much analysis, I'd say that you have fallen victim to a very common error for new programmers. A bool is either true or false. It can also be ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Segmentation fault in find.c

There are two problems - an infinite loop and a parameter that is out of range. Infinite loop: In binarysearch(), when the target is not in the list, the recursive calls will eventually get to a ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Check50 is claiming that it's recieving an exit code of 0 rather than 1 (different scenario)

This code will never find anything unless value == midpoint. On each pass through the loop, it updates the min or the max, but it never updates midpoint. Since midpoint never changes, the loop will ...
Cliff B's user avatar
  • 69k
1 vote

Stuck in loop on Binary Search

Keep in mind that = is an assignment operator. It assigns right value to left value. Any subsequent changes to that right value won't have an effect on left value unless you assign it again. Same ...
aquablitz11's user avatar
1 vote

Pset 3 - Binary Search - clang: error: linker command failed with exit code 1

The error message means the term "search" is not defined. Did you change the function name in helpers.c? It looks like you have "binarySearch" instead of "search".
ronga's user avatar
  • 1,415
1 vote
Accepted

check50 fails to compile helpers.c - throws implicit function declaration error

Header files act as a snippet informing programs that use it, "Okay. I've prepared these functions for you. You don't have to know how it works under the hood. It's not your business if I'm secretly ...
aquablitz11's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible