Reputation: 8977
So the question is like this:
Given a location X and an array of locations, I want to get an array of locations that are closest to location X, in other words sorted by closest distance.
The way I solved this is by iterating through each location array and calculate the distance between X and that particular location, store that distance, and sort the location by distance using a Comparator. Done! Is there a better way to do this? Assuming that the sort is a merge sort, it should be O(n log n).
Upvotes: 6
Views: 2791
Reputation: 32532
The problem you describe sounds like a m
-nearest neighbor search to me.
So, if I correctly understood your question, i.e. the notion of a location
being a vector in a multidimensional metric space, and the distance
being a proper metric in this space, then it would be nice to put the array of locations
in a k
-d-Tree.
You have some overhead for the tree building once, but you get the search for O(log n)
then.
A benefit of this, assuming you are just interested in the m < n
closest available locations, you don't need to evaluate all n
distances every time you search for a new X
.
Upvotes: 2
Reputation:
Proof by contradiction that you can't do better:
It is well known that comparison-based sorts are the only way to sort arbitrary numbers (which may include irrational numbers), and that they can't do better than n*log(n) time.
If you go through the list in O(n) time and select the smallest number, then use that as X, and somehow come up with a list of numbers that are sorted by distance to X, then you have sorted n numbers in less than O(n*log(n)) time.
Upvotes: 0
Reputation: 640
If I understand this right, you can do this pretty quickly for multiple queries - as in, given several values of X, you wouldn't have to re-sort your solution array every time. Here's how you do it:
This takes O(n + logn) for each query with O(nlogn) preprocessing. Of course, if we were talking about just one X, this is not better at all.
Upvotes: 2
Reputation: 28302
You can't do asymptotically better than O(n log n) if using a comparison-based sort. If you want to talk about micro-optimization of the code, though, some ideas include...
A thinking-outside-the-box solution might be to use e.g. Bucket sort... A linear time sorting algorithm which might be applicable here.
Upvotes: 0
Reputation: 7964
You should try using min-heap ds to implement this. Just keep on storing the locations in heap with key = diff of X and that location
Upvotes: 0