# Dynamic algorithm

• I'm doing a game. There's a dynamic set of objects displayed. (Objects are added and removed in the course of the game) The challenge is to do something like the principle of isotomy. The graphic objects "y"the less will be under the ones "yMore. I'll change. ChildIndex

Please give me an algorithm. Oh, yeah.

• In this case, you need not so much the appropriate algorithm of sorting as the appropriate data structure. It's called balanced binary tree

The data in such a tree are stored in an adorable form, so the binary search provides time O(log N). Unlike the mass, the data entry and disposal into the binary tree requires O(log N) to replace O(N). The balance is saved from birth when the tree is actually becoming a list.

There are different ways to support the tree in a balanced manner, in particular Red Black trees and AVL trees

In general, a balanced binary tree has already been implemented in the standard library, so find the appropriate data structure. It could be something. `SortedDictionary` or `std::map`

UPDATE

Simple things look like if you need classified data, you can store them in the mass and sort them whenever you need them. The temporal complexity of the general algorithms of sorting is O(Nxlog N). This record means that (simplified) with an increase in the size of the mass k once, the time of work of the algorithm will increase proportionally (kxN)xlog(kxN). It's not a very good speed, because if the size of the mass grows 10 times, the timing of its sorting increases, say, 30 times.

If you need this mass all the time, for example, for a quick binary search, it makes sense to sort it at the beginning and then use it. The binary search allows the elements to be found during O(log N), and this is very fast. In a thousand elements, you'll find the necessary for only 10 matches, and that's the worst case.

The situation is complicated if it is necessary to add and remove elements into the body during the operation. Let's say we need to insert a new element that's not in the body. First, we need to find him, and if we haven't found him, put him in the right place to keep the mass scattered. The search operation takes O(log N) time, as we have already said, and the element box in the middle of the area takes O(N) time. This is again a long time: an average of half the mass, i.e. some 500 elements, will need to be moved every time to put the element into a mass of 1,000 elements.

We'll have to move an average half of the mass again to remove it. If supply and disposal operations occur regularly, they must be done quickly. One option is to use the data structure, the supply and disposal of which is performed very quickly. One of the appropriate structures is an orderly binary tree.binary tree)

Each tree has the meaning of the element and two references: left support and right support. Element in the root of left support is less than the current element, and the element at the root of the right is bigger.

The search, supply and removal of such wood shall be carried out during O(log N). What we need. One Trouble: if the tree is to be used to install already dissorted elements, they will all be in left (or right) support. There'll be a congenital twin tree, where all the left supports are empty, and the tree goes right.

The search, supply and removal of the congenital tree will again be carried out in slow time O(N). In order to solve this problem, the tree is balancing, that is, from time to time it is rebuilt so that the left and right supports are approximately equal to the elements (simplified).

The balance is time-consuming, but it always allows for the identification, insertion and removal of the element over time prorated by O(log N).

There are different ways of balancing that I've already written above. It's simple to googling them, and they're well described in Russian and other resources. It is also useful to read one of the most classic books on this topic: Niklaus Virt, Algorithms and data structures. For the sake of ease of corn, I gave in this reply the terms that could be googled. inclined type

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2