An implementation of Q sort that tries to be memory efficient