Dynamic array with O (1) deletion of any element

This question is about the data structure that I was thinking about. This is a dynamic array, for example std :: vector <> in C ++, except that the deletion algorithm is different.

In a normal dynamic array, when an element is deleted, all other elements must be shifted down, which is O (n), unless it is the last element to be O (1).

In this case, if any item is deleted, it is replaced with the last item. This, of course, loses the order of the elements. But now deleting any item is a constant time.

The list will have the same deletion time, but this structure has random access. The only caveat is that you don’t know what you are referring to, since the ordering can be confused, so use random access anyway. In addition, the list will not confuse any pointers / iterators with elements.

So, this structure seems useless, except for the very specific task of strictly following the elements and possibly deleting them along the way. A list can do the same, but it has better cache performance.

So does this weird / useless structure have a name and does it use any uses? Or just a nice little brainstorm?

+5
source share
6 answers
+5

, / - ?

- .

, , , , . .

, , .

V-- waiting
[ A-active, B-active, C-active, D-active ]
                             completed --^
^- run

, . , , .

           V-- waiting
[ A-waiting, B-active, C-active, D-active ]
                              completed --^
             ^- run

, , .

           V-- waiting
[ A-waiting, D-active, C-active, B-completed ]
                   completed --^
             ^- run

, , , .

                      V-- waiting
[ A-waiting, C-waiting, D-active, B-completed ]
                   completed --^
                        ^- run

:

                      V-- waiting
[ A-waiting, C-waiting, D-completed, B-completed ]
          completed --^
                        ^- run == completed so stop

, swapping , "" .

+3

, . .

: " " .. , , - ( , 99%). , , .

Binary Heap , - O (log n).

+2

, , .

, . , .

0

Hm, O (1)?

,

  • - O (1)
  • ( ) - O (1)
  • - ( "" ) - O (1)

... , . , , .

-1
-1

All Articles