Sunday, January 11, 2015

You are wrong, not the library code

I was just reading through an algorithms book about quicksort, and stepping through the code, tracing through how it would sort "easyquestion". I ran into an infinite loop problem, and rechecked my steps several times. I assumed the algorithm in this book was wrong, and decided to implement it and run it to prove that it wasn't working. To my surprise it was working! I put a debugging condition in the spot where i was running into the infinite loop on paper, and realized my mistake. I mistakenly thought a break statement was breaking out of the outer loop, not one of the inner loops, resulting in bad index pointers, causing infinite recursion.

My mistake was quite stupid, and I was reading the code slightly wrong. This is the opposite of a typo. The underlying mistake though was that I assumed the book was wrong. Always assume your code is wrong, not the other guy's, until you have exhaustively checked and rechecked every possibility in your own code. 

No comments:

Post a Comment

There was an error in this gadget