CS50 pset3 find


#1

Can anyone point out what I did wrong? Not sure what it is. Thanks!


#2

Hi,

In case you’re still looking for some help with your code, here’s a few things I’ve noticed…

  • Your sort method sorts the array elements in descending order, and it’s not going all the way because you’re stopping at index n-2, so the last element (at index n-1) is never sorted. Change your loop stop condition to:

    for (int i = 0; i < n; i++)

  • Since your array is now sorted in descending order, when you’re searching for an element, and partitioning your array in half, the biggest elements are in the left half of the array, and the smallest are in the right half of the array. Which means that instead of testing whether:

    else if (values[mid] > value) { end = mid -1 }

    your test becomes: else if (values[mid] < value) { end = mid -1 }

I hope to have been of help. Happy coding! :smiley: