rehashing of nn algorithm