Algorithm to keep collection sorted while inserting in the middle -
Suppose I have a large collection of elements.
Each element has a "status" field, which is a positive integer
The area of two elements is not the same value for the "position"
The only supported operation is the collection: addElement (newElement, positionAfterElement), where:
- newElement is a new element to be added (Its status is unknown for now)
positionAfterElement is an existing element of the collection
The function will guarantee that:
- Status (status interruptible) & lt; Status (New Element)
- The position between another element (position interval) and position (new element) in the collection is
I want to change the value of all element posts but I want to reduce the number of changes (on average).
How do I implement the Adilegment function?
I can only extend all elements together with all the positions from 1 but I am absolutely sure that there should be a better way of doing this.
Thank you for your help.
Use a balanced tree. On each node of the tree, calculate the number of items given below (left qount + right.count + 1) After that, you can calculate an item when indexing it easily on it. In the number of operations it is time of O (n log n).
Comments
Post a Comment