# What is the quickest way to allocate a set of points to a fixed number groups based on Euclidean distances from a point in each group?

Example, if i have certain geographic points in an array A=[a,b,c,d...n] and have 10 points distributed in the solution space B= [1,2,3...10], how can I allocate each member of A to the nearest point in B in the quickest way.

• Welcome to the site! Programming questions are off-topic, here. We're happy to help with algorithms but implementing something in a specific language isn't something we do, here. – David Richerby Sep 15 '16 at 15:51
• Are the values in A numeric, or characters as you have typed? How can you define what the "nearest point" is in this situation? – Jaken Herman Sep 15 '16 at 19:32
• What do you mean by "geographic points:? by "solution space"? What have you tried? What approaches have you considered, and why did you reject them? – D.W. Sep 16 '16 at 1:33
• @David this can be in any language. @ Jaken the values in A are geographic coordinates for example (120.2, 130.5) representing x and y respectively. The same case goes for the values in B. By nearest point, I meant for the values in A which pint is closest in terms of Euclidean distance – David Sep 16 '16 at 8:36
This looks very much like the Nearest neighbor problem. The asymptotically most efficient approach is probably computing a k-d-tree on the points in B. Constructing the tree takes $O(|B| \log |B|)$ and lookup is logarithmic, so an additional $O(|A| \log |A|)$ to find all nearest neighbours.