bubbleSort misprint
algor opened this issue · comments
Hi, Nicholas.
There is an error (misprint) in bubbleSort algorithm that makes unnecessary comparison beyond the range of items in each iteration.
var len = items.length
should be
var len = items.length - 1.
For example, having five items in the list, the last value for j index during first iteration must be 3 and not 4. Because we need to compare fourth item in the list (index 3) with the fifth item in the list (index 4).
The easiest way to see the issue is to put console.log(j, items[j], items[j+1]) in the inner loop and observe that for the first iteration the items[j+1] prints "undefined".
The same issue is in the article at http://www.nczonline.net/blog/2009/05/26/computer-science-in-javascript-bubble-sort/
Thanks
Alexei