fixed a memory access bug in the sorting algorithm