# How to achieve fast "vector clustering" sort?

**URL:** <https://discuss.pytorch.org/t/how-to-achieve-fast-vector-clustering-sort/138844>\
**Category:** Uncategorized\
**Created:** [December 8, 2021, 11:06am UTC](https://discuss.pytorch.org/t/how-to-achieve-fast-vector-clustering-sort/138844 "2021-12-08T11:06:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![111414](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111414/32/31166_2.png) [@111414](https://discuss.pytorch.org/u/111414)\
**Post date:** [December 8, 2021, 11:06am UTC](https://discuss.pytorch.org/t/how-to-achieve-fast-vector-clustering-sort/138844/1 "2021-12-08T11:06:54Z")

</div>

I have n c-dimensional vectors that formed into a matrix A with the shape of (n, c), how can I perform a quick sort such that the vectors with low Euclidean distances are as close as possible, and the vectors with high distances are as far as possible?  
For example, I have

```auto
A = [[0, 3], [0, 0], [0, 1]],

```

and the solution can be

```auto
A_sorted = [[0, 3], [0, 1], [0, 0]].

```

Explain: Because the original A has a total weighted distance sum of 3x1+1x1+2x2 = 7, and A\_sorted has 2x1+1x1+3x2 = 8.  
Mathematically, the goal is to maximize the total weighted distance sum.  
For the 1-dimensional case, this can be achieved by torch.sort(), and my main concern is if there exists a fast implementation when c ≥ 2 with a time complexity of O(nlog(n))? 🤔  
After a long struggle, I failed. 😅 Could you please do me a favor?

---

<div class="post-metadata">

**Author:** ![111414](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111414/32/31166_2.png) [@111414](https://discuss.pytorch.org/u/111414)\
**Post date:** [December 8, 2021, 12:53pm UTC](https://discuss.pytorch.org/t/how-to-achieve-fast-vector-clustering-sort/138844/2 "2021-12-08T12:53:58Z")

</div>

It seems that this is as hard as Traveling Salesman Problem (I’m not sure), so sad. 😰

---

<div class="post-metadata">

**Author:** ![111414](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111414/32/31166_2.png) [@111414](https://discuss.pytorch.org/u/111414)\
**Post date:** [December 24, 2021, 1:26am UTC](https://discuss.pytorch.org/t/how-to-achieve-fast-vector-clustering-sort/138844/3 "2021-12-24T01:26:34Z")

</div>

I found that there is a similar question: [Link](https://stackoverflow.com/questions/37742358/sorting-points-to-form-a-continuous-line). 😅  
Does there exists a fast PyTorch implementation that supports batch processing? (I have B instances, each instance contains N C-dimensional points, i.e. the shape of input data is [B, C, N]) 🤔

---

<div class="post-metadata">

**Author:** ![111414](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111414/32/31166_2.png) [@111414](https://discuss.pytorch.org/u/111414)\
**Post date:** [December 24, 2021, 1:40am UTC](https://discuss.pytorch.org/t/how-to-achieve-fast-vector-clustering-sort/138844/4 "2021-12-24T01:40:06Z")

</div>

I think it’s hard to directly find the optimal results, so I am pursuing a weaker solution which is fast and can find results that may not be optimal but better than random permutation or just calculating their L2 norm (distance from [0,0]) and sort, at least. 🤔
