What's wrong with this Average Magnitude Difference algorithm?
okay, i took a look at your code and at your data.

first of all, the algorithm you are using is not autocorrelation but is the Average Magnitude Difference Function (AMDF).  it will give results similar to the autocorrelation but upside-down.

you appear to be implementing the AMDF correctly but you are suffering from the so-called "octave error" problem.  you see, a note that is, say, A-440, has a fundamental frequency of 440 Hz.  that means the waveform repeats every $\tfrac{1}{440}$-th second.  but that same waveform also repeats every $\tfrac{1}{220}$-th second and every $\tfrac{1}{110}$-th second or every $\tfrac{1}{55}$-th second.  so, mathematically, it can be construed to be a 220 Hz or 110 Hz or 55 Hz (or even a $\tfrac{3}{440}$-th second period or $\tfrac{440}{3}$ Hz waveform).

so you need to figure out how to turn your AMDF upside down and offset it to get peaks instead of valleys.  for the math relating AMDF to ASDF and that to autocorrelation, take a look at this and particularly this.  you have multiple pitch candidates and you have to elect the correct candidate.  don't do it like the Americans did last November.