Sort array of locations by closest point

So the question is this:

Given 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 to repeat through each array the locations and calculate the distance between X and that particular location, save that distance and sort the location by distance using the comparator. Done! Is there a better way to do this? Assuming the sort is a merge sort, it should be O (n log n).

+5
source share
5 answers

, - , X, . :

  • (O (n logn) - )
  • , X, X ( X). , j, , . , , X . . , , - j , j. , .

O (n + logn) O (nlogn). , , .

+2

, , m -newest search search .

, , .. a location , distance ​​ , array of locations k -d-Tree. , O(log n) .

, , m < n, n X.

+2

min-heap ds . = diff X

0

, O (n log n), . - , ...

  • ; - sqrt() - sqrt()
  • ; | dx1 | <= | dx2 | | dy1 | <= | dy2 |, pt1 , pt2 - ,

, , ... , .

0

: :

, - ( ) , n * log (n).

O (n) , X - , X, n O (n * log (n)).

0

All Articles