简体   繁体   English

在更改随机元素的同时保持排序

[英]Maintaining sort while changing random elements

I have come across this problem where I need to efficiently remove the smallest element in a list/array. 我遇到了这个问题,我需要有效地删除列表/数组中的最小元素。 That would be fairly trivial to solve - a heap would be sufficient. 那将是相当微不足道的解决-堆就足够了。

However, the issue now is that when I remove the smallest element, it would cause changes in other elements in the data structure, which may result in the ordering being changed. 但是,现在的问题是,当我删除最小的元素时,它将导致数据结构中其他元素的更改,这可能导致顺序更改。 An example is this: 一个例子是这样的:

I have an array of elements: 我有一系列元素:

[1,3,5,7,9,11,12,15,20,33]

When I remove "1" from the array "5" and "12" get changed to "4" and "17" respectively. 当我从数组“ 5”和“ 12”中删除“ 1”时,分别更改为“ 4”和“ 17”。

[3,4,7,9,11,17,15,20,33]

And hence the ordering is not maintained. 因此,不维持订购。

However, the element that is removed will have pointers to all elements that will be changed, but there is not knowing how many elements will be changed and by how much. 但是,被删除的元素将具有指向将要更改的所有元素的指针,但是尚不知道将更改多少元素以及更改多少。

So my question is: 所以我的问题是:

What is the best way to store these elements to maximize performance when removing the smallest element from the data structure while maintaining sort? 当从数据结构中删除最小的元素并保持排序时,存储这些元素以最大化性能的最佳方法是什么? Or should I just leave it unsorted? 还是我应该不做任何排序?

My current implementation is just storing them unsorted in a vector, so the time complexity is O(N^2), O(N) for finding the smallest element, and N removals. 我当前的实现方式是将它们不排序地存储在向量中,因此时间复杂度为O(N ^ 2),O(N)(用于查找最小元素)和N个移除。

A. 一种。

If you have the list M of all changed elements of the ordered list L, 如果您拥有有序列表L中所有已更改元素的列表M,

  • go through M, and for every element 经历M,并且针对每个元素

  • If it is still ordered with its neigbours in M, live it be. 如果仍然与M的邻居一起订购,那就活着吧。

  • If it is not in order with neighbours, exclude it from the M. 如果与邻居不符,则将其从M中排除。

  • Such excluded elements will create a list N 这样的排除元素将创建一个列表N

  • Order N N号

  • Use some algorithm for merging ordered lists. 使用某种算法来合并有序列表。 http://en.wikipedia.org/wiki/Merge_algorithm http://en.wikipedia.org/wiki/Merge_algorithm

B. B.

If you are sure that new elements are few and not strongly changed, simply use the bubble sort. 如果您确定新元素很少并且没有强烈更改,只需使用冒泡排序即可。

I would still go with a heap ,backed by an array 我仍然会去堆,并得到数组的支持

In case only a few elements change after each pop,After you perform the pop operation , perform a heapify up/down for any item that reduces in value. 如果每次弹出后只有几个元素发生更改,请执行弹出操作后,对任何值减小的项目执行上/下堆放大。 It will still be in the order of O(nlog k) values, where k is the size of your array and n the number of elements that have reduced in size. 它仍将是O(nlog k)值的顺序,其中k是数组的大小,n是已减小大小的元素的数量。

If a lot of items change in size , then you can consider this as a case where you have an unsorted array and you just create a heap from the array. 如果很多项目的大小发生变化,则可以将其视为未排序的数组,而仅从该数组创建堆的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM