Added constant memory consumption sort function