There's a very efficient library for searching nearest neighbours in arbitrary dimensions. Have a look
here
Internally, it uses different search strategies like kd-trees etc that you rather don't want to code yourself if you don't have to. I've tried it, works great.