给定一个包含N个点和一个参考点P的数组arr [] ,任务是根据它们与给定点P的距离对这些点进行排序。
例子:
Input: arr[] = {{5, 0}, {4, 0}, {3, 0}, {2, 0}, {1, 0}}, P = (0, 0)
Output: (1, 0) (2, 0) (3, 0) (4, 0) (5, 0)
Explanation:
Distance between (0, 0) and (1, 0) = 1
Distance between (0, 0) and (2, 0) = 2
Distance between (0, 0) and (3, 0) = 3
Distance between (0, 0) and (4, 0) = 4
Distance between (0, 0) and (5, 0) = 5
Hence, the sorted array of points will be: {(1, 0) (2, 0) (3, 0) (4, 0) (5, 0)}
Input: arr[] = {{5, 0}, {0, 4}, {0, 3}, {2, 0}, {1, 0}}, P = (0, 0)
Output: (1, 0) (2, 0) (0, 3) (0, 4) (5, 0)
Explanation:
Distance between (0, 0) and (1, 0) = 1
Distance between (0, 0) and (2, 0) = 2
Distance between (0, 0) and (0, 3) = 3
Distance between (0, 0) and (0, 4) = 4
Distance between (0, 0) and (5, 0) = 5
Hence, the sorted array of points will be: {(1, 0) (2, 0) (0, 3) (0, 4) (5, 0)}
方法:想法是将每个元素与距给定点P的距离成对存储,然后根据存储的距离对向量的所有元素进行排序。
- 对于每个给定点:
- 使用以下公式找到该点与参考点P的距离:
距离=
- 在数组中追加距离
- 使用以下公式找到该点与参考点P的距离:
- 对距离数组进行排序,并根据排序后的距离打印点。
下面是上述方法的实现:
C++
// C++ implementation to sort the // array of points by its distance // from the given point #include
using namespace std; // Function to sort the array of // points by its distance from P void sortArr(vector > arr, int n, pair p) { // Vector to store the distance // with respective elements vector > > vp; // Storing the distance with its // distance in the vector array for (int i = 0; i < n; i++) { int dist = pow((p.first - arr[i].first), 2) + pow((p.second - arr[i].second), 2); vp.push_back(make_pair( dist, make_pair( arr[i].first, arr[i].second))); } // Sorting the array with // respect to its distance sort(vp.begin(), vp.end()); // Output for (int i = 0; i < vp.size(); i++) { cout << "(" << vp[i].second.first << ", " << vp[i].second.second << ") "; } } // Driver code int main() { // Array of points vector > arr = { { 5, 5 }, { 6, 6 }, { 1, 0 }, { 2, 0 }, { 3, 1 }, { 1, -2 } }; int n = 6; pair p = { 0, 0 }; // Sorting Array sortArr(arr, n, p); return 0; }
Java
// Java implementation to sort the // array of points by its distance // from the given point import java.util.*; class Pair
{ K first; V second; Pair(K a, V b) { first = a; second = b; } } class GFG{ // Function to sort the array of // points by its distance from P static void sortArr(ArrayList > arr, int n, Pair p) { // Vector to store the distance // with respective elements ArrayList >> vp = new ArrayList<>(); // Storing the distance with its // distance in the vector array for(int i = 0; i < n; i++) { int dist = (int)Math.pow( (p.first - arr.get(i).first), 2) + (int)Math.pow( (p.second - arr.get(i).second), 2); vp.add( new Pair >( dist, new Pair ( arr.get(i).first, arr.get(i).second))); } // Sorting the array with // respect to its distance Collections.sort(vp, (a, b) -> a.first - b.first); // Output for(int i = 0; i < vp.size(); i++) { System.out.print("(" + vp.get(i).second.first + ", " + vp.get(i).second.second + ") "); } } // Driver code public static void main(String[] args) { // Array of points int a[][] = { { 5, 5 }, { 6, 6 }, { 1, 0 }, { 2, 0 }, { 3, 1 }, { 1, -2 } }; ArrayList > arr = new ArrayList<>(); for(int i = 0; i < a.length; i++) arr.add(new Pair (a[i][0], a[i][1])); int n = 6; Pair p = new Pair (0, 0); // Sorting Array sortArr(arr, n, p); } } // This code is contributed by jrishabh99
Python3
# Python3 implementation to sort the # array of points by its distance # from the given point # Function to sort the array of # points by its distance from P def sortArr(arr, n, p): # Vector to store the distance # with respective elements vp = [] # Storing the distance with its # distance in the vector array for i in range(n): dist= pow((p[0] - arr[i][0]), 2)+ pow((p[1] - arr[i][1]), 2) vp.append([dist,[arr[i][0],arr[i][1]]]) # Sorting the array with # respect to its distance vp.sort() # Output for i in range(len(vp)): print("(",vp[i][1][0],", ",vp[i][1][1], ") ",sep="",end="") # Driver code arr = [[5, 5] , [6, 6] , [ 1, 0] , [2, 0] , [3, 1] , [1, -2]] n = 6 p = [0, 0] # Sorting Array sortArr(arr, n, p) # This code is contributed by shivanisinghss2110
输出:(1, 0) (2, 0) (1, -2) (3, 1) (5, 5) (6, 6)
性能分析:
- 时间复杂度:与上述方法一样,对长度为N的数组进行排序,在最坏的情况下,这需要O(N * logN)时间。因此,时间复杂度将为O(N * log N) 。
- 辅助空间复杂度:与上述方法一样,存在额外的空间用于将距离和点成对存储。因此,辅助空间复杂度将为O(N) 。