CS50 Overload

Daily Standup

It’s always the smallest things that can make your code not work!!

Binary Search Algorithm

Today for me is was using less than < instead of less than or equals <=…as a result my binary search algorithm could only find its key in the top half of a sorted array, and returned false if the sought value was in the bottom half. I’m glad I figured out that it was even working in any way–the original test failed–as this made it easier to narrow down what the problem was. In the end I got the function to search both halves correctly:

// If value is in an array return true, else return false
// where value is an integer and values is an array of integers

function binarySearch(value, values) {
while (values.length > 0) {
int l = 0, // Set left, right indices
r = values.length - 1;
while (l <= r) { // Terminate if left & right bounds cross over each other
int m = (l + r) / 2; // Set middle index
if (value == values[m]) {
return true;
} else if (value > values[m]) {
l = m + 1; //Throw out bottom half if greater
} else if (value < values[m]) {
r = m - 1; // Throw out top half if lower
}
}
return false;
}
return false;
}

Game of Fifteen

Next was writing a Game of 15 puzzle in C which can be played from the console. I hit so many brick walls I literally cannot write about or even think about this anymore! But I got it done : D

When I finally finished I was so excited I thought, “oh now I’ll write it in JavaScript!” On second thought…

Other Stuff

I came across a video of Mark Zuckerberg doing a Q&A at CS50 in 2005. It was interesting to hear him talk about what it was like building the first iterations of (The) Facebook–the data structures, size limitations, social concerns, etc. It’s long (one to watch on double time) but interesting!

Up Next

The next CS50 lecture & homework set is all about working with memory, recovering data, etc. I know very little about this so interested to understand more.