发现第n最小元素以阵列阵列、最小、素以、发现

2023-09-11 04:16:17 作者:—Lonly·迷恋

可能重复:   How找到o在长度为n的排序的数组的第k最大元素(N)?

我目前正坐在一个课程作业的前面。 的任务是找到一个数组中第n最小元素。 (不排序它!)

I'm currently sitting in front of an course assignment. The task is to find the nth-smallest element in an array. (Without sorting it!)

我想了解BFPRT算法,但是从我得到了它是唯一有用的,如果你要计算的位数,而不是第n个最小元素。

I tried to understand the BFPRT algorithm but from what I got it is only useful if you want to calculate the median and not the "n-th smallest" element.

另一个想法我不得不,是通过将较小/较大节点到根节点的左/右的数组转换成树。我不知道但是如果这算作排序。 为了加速此我可以存储在每个节点的子节点的数量。

Another idea I had, was to convert the array into a tree by attaching smaller/bigger nodes to the left/right of the root node. I'm not sure however if this counts as sorting. To accelerate this I could store the number of subnodes in each node.

完整的任务还包括,该算法是递归的。 也有提示去想其他的数据结构。

The complete assignment also includes that the algorithm has to be recursive. There is also the hint to think about other data structures.

你觉得我的转化数组复制到一个平衡树的主意?

What do you think about my idea of transforming the array into a balanced tree?

是否还有其他的选择,我可能会错过?

编辑:我看了一下各种类似的问题,但没能完全理解的答案/它们应用到我的具体任务

I looked at various similar questions but were not able to completely understand the answers/apply them to my specific task.

推荐答案

传统的办法处理这一问题(的顺序统计问题的)让人想起快速排序的。比方说,你正在寻找的 K 的'个最小元素。挑选一个(随机)枢轴元件和分区其余元件分成两组(不含排序两组):大号的包含小于或等于所述枢轴元件的所有元素(除了枢轴元件本身),和的