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

Given an array **arr[]** containing **N** points and a reference point **P**, the task is to sort these points according to it’s 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 with 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 point:
- Find the distance of the point from the reference point P using the below formulae:
Distance =

- Append the distance in an array

- Find the distance of the point from the reference point P using the below formulae:
- Sort the array of distance and print the points based on the sorted distance.

Below is the implementation of the above approach:

## C++

`// C++ implementation to sort the ` `// array of points by its distance ` `// from the given point ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to sort the array of ` `// points by its distance from P ` `void` `sortArr(vector<pair<` `int` `, ` `int` `> > arr, ` ` ` `int` `n, pair<` `int` `, ` `int` `> p) ` `{ ` ` ` ` ` `// Vector to store the distance ` ` ` `// with respective elements ` ` ` `vector<pair<` `int` `, ` ` ` `pair<` `int` `, ` `int` `> > > ` ` ` `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<pair<` `int` `, ` `int` `> > arr ` ` ` `= { { 5, 5 }, { 6, 6 }, { 1, 0 }, { 2, 0 }, { 3, 1 }, { 1, -2 } }; ` ` ` `int` `n = 6; ` ` ` `pair<` `int` `, ` `int` `> p = { 0, 0 }; ` ` ` ` ` `// Sorting Array ` ` ` `sortArr(arr, n, p); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

(1, 0) (2, 0) (1, -2) (3, 1) (5, 5) (6, 6)

**Performance Analysis:**

**Time Complexity:**As in the above approach, there is sorting of an array of length N, which takes O(N*logN) time in 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 pair. Hence the auxiliary space complexity will be**O(N)**.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Pick points from array such that minimum distance is maximized
- Ways to choose three points with distance between the most distant points <= L
- Sort integers in array according to their distance from the element K
- Check if it is possible to sort an array with conditional swapping of elements at distance K
- Program to calculate distance between two points in 3 D
- Program for distance between two points on earth
- Program to calculate distance between two points
- Hammered distance between N points in a 2-D plane
- Distance between a point and a Plane in 3 D
- Find points at a given distance on a line of given slope
- Find the maximum possible distance from origin using given points
- Check whether it is possible to join two points given on circle such that distance between them is k
- Find the number of points that have atleast 1 point above, below, left or right of it
- Find if a point lies inside, outside or on the circumcircle of three points A, B, C
- Perpendicular distance between a point and a Line in 2 D
- Shortest distance between a point and a circle
- Haversine formula to find distance between two points on a sphere
- Find the integer points (x, y) with Manhattan distance atleast N
- Distance between end points of Hour and minute hand at given time
- Find integral points with minimum distance from given set of integers using BFS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.