算法 - 首先对大多数重复的数字进行排序(降序)
我有一个数字列表,例如:
10 4
5 3
7 1
-2 2
第一行是指数字10
重复4
次,第二行是指数字5
重复三个等等。目的是对这些数字进行排序,这是以下降顺序首先重复的。我认为使用hashmap记录数据,然后将其馈送到树上,然后按值排序将是最有效的方法 - > o(n log n),但是有更有效的方法吗?我听说使用Max-Heap解决了这个问题,但我认为堆的做得不如O(n log n)更好。
I have a list of numbers, for example:
10 4
5 3
7 1
-2 2
first line means the number 10
repeats 4
times, the second line means the number 5
repeats thrice and so on. The objective is to sort these numbers, the most repeated first in descending order. I think using Hashmap to record the data and then feeding it to treeset and sort by value would be the most efficient way -> O(n log n), but is there a more efficient way? I've heard this problem is solved with max-heap, but I dont think heap can do better than O(n log n).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为,通过铲斗式排序,o(n)的复杂性是可能的。至少在理论上。但是,在存储桶的内存方面,它带来了额外的成本。这可能会使方法在实践中棘手。
水桶是标签。每个水桶都以相同的计数保存所有数字。为了快速访问,我们将存储桶保存在阵列列表中,每个存储库处于其计数的索引位置。
像OP一样,我们使用hashmap将数字与计数器相关联。当一个数字到达时,我们将计数器增加,并将数字从旧计数的存储库移至新计数的存储桶中。这始终保持数字始终排序。
每个到达的数字需要O(1)进行处理,因此全部采用O(n)。
I think with bucket-style sorting, O(N) complexity is possible. At least in theory. But it comes with additional cost in terms of memory for the buckets. That may make the approach intractable in practice.
The buckets are HashSets. Each bucket holds all numbers with the same count. For fast access, we keep the buckets in an ArrayList, each bucket at the index position of its count.
Like the OP, we use a HashMap to associate numbers with counters. When a number arrives, we increment the counter and move the number from the bucket of the old count to the bucket of the new count. That keeps the numbers sorted at all times.
Each arriving number takes O(1) to process, so all take O(N).
您可以获得
map.entry< integer,Integer>
的排序列表,例如:它应该在o(n*log(n))中。
You can get a sorted list of
Map.Entry<Integer,Integer>
like this:It should be in O(n*log(n)).
(我替换了我的原始答案。)
geekforgeeks的
hashmap
按值。由于无法对
hashmap
进行排序,并且在linkedhashmap
中的元素顺序无法更改,除非将其删除和以不同的顺序重新输入代码将内容复制到list
中,然后对列表进行排序。作者希望将数据保留在hashmap
中,因此将排序列表复制到新的linkedhashmap
。出于O/P的目的,将在示例具有
&lt; string,Integer&gt;
的情况下使用&lt; integer,integer&gt;
。另外,由于所需的订单正在下降,o2
和o1
将在比较器的return
语句中反转:有数学证据,没有比较排序的运行少于O(N log N)。
(I Replaced my original answer.)
GeekForGeeks has example code for sorting a
HashMap
by value.Since a
HashMap
can't be sorted, and the order of elements in aLinkedHashMap
can't be changed unless they are removed and re-entered in a different order, the code copies the contents into aList
, then sorts the list. The author(s) want to retain the data in aHashMap
, so the sorted list is then copied to a newLinkedHashMap
.For the O/P's purposes,
<Integer, Integer>
would be used where the example has<String, Integer>
. Also, since the desired order is descending,o2
ando1
would be reversed in thereturn
statement of the comparator:There are mathematical proofs that no comparison sort can run in less than O(n log n).
假设数字是唯一的,
为什么要排序?只需循环遍历每个键,保存最大事件,您就可以在O(n)中获得它
Assuming the number is unique,
Why sort? just loop through every key save max occurence, and you’ll get it in O(n)