Sort an Array of Points by their distance from a reference Point - GeeksforGeeks

26 Nov, 2021
Given an array arr[] containing N points and a reference point P, the task is to sort these points according to their distance from the given point P.
Examples:
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)}
Approach: The idea is to store each element at its distance from the given point P in a pair and then sort all the elements of the vector according to the distance stored.
For each of the given points: Find the distance of the point from the reference point P formula below:
Find the distance of the point from the reference point P formula below:
Distance = 
Append the distance in an array
Sort the array of distance and print the points based on the sorted distance.Time Complexity: As in the above approach, there is sorting of an array of length N, which takes O(N*logN) time in the worst case. Hence, the Time Complexity will be O(N*log N).Auxiliary Space Complexity: As in the above approach, there is extra space used to store the distance and the points as pairs. Hence, the auxiliary space complexity will be O(N).
Time Complexity: As in the above approach, there is sorting of an array of length N, which takes O(N*logN) time in the worst case. Hence, the Time Complexity will be O(N*log N).
Auxiliary Space Complexity: As in the above approach, there is extra space used to store the distance and the points as pairs. Hence, the auxiliary space complexity will be O(N).
Javascript
<script>// Javascript program function sortFunction(a, b) {    if (a[0] === b[0]) {        return 0;    }    else {        return (a[0] < b[0]) ? -1 : 1;    }} // Function to sort the array of// points by its distance from Pfunction sortArr(arr, n, p){    // Vector to store the distance    // with respective elements         var vp = new Array(n);    // Storing the distance with its    // distance in the vector array    for (var i = 0; i < n; i++) {           var dist = Math.pow((p[0] - arr[i][0]), 2)              + Math.pow((p[1] - arr[i][1]), 2);        vp[i] = [dist, [arr[i][0], arr[i][1]]];    }         // Sorting the array with    // respect to its distance    vp.sort(sortFunction);       // Output    for (var i = 0; i < n; i++) {        document.write("(" + vp[i][1][0] + ", " + vp[i][1][1] + ") ");    }} var arr = [[ 5, 5 ], [ 6, 6 ], [ 1, 0], [ 2, 0 ], [ 3, 1 ], [ 1, -2 ]];var n = 6;var p = [ 0, 0 ];// Function to perform sortingsortArr(arr, n, p);</script>
Output:
(1, 0) (2, 0) (1, -2) (3, 1) (5, 5) (6, 6) 
shivanisinghss2110
jrishabh99
rishavmahato348
cpp-pair
Arrays
Geometric
Mathematical
Arrays
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stack Data Structure (Introduction and Program)
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
How to check if two given line segments intersect?
Program for distance between two points on earth
How to check if a given point lies inside or outside a polygon?
Find if two rectangles overlap
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)