P
P
Porto_b2019-12-03 19:02:01
Algorithms
Porto_b, 2019-12-03 19:02:01

What's wrong with inserting into an array at a given position?

For example, I wrote a program to insert the key element at a given position in the array. The code itself works, but there was a question. Wouldn't it be better to first go from the end of the array, rearranging the elements, and at the end just write a[pos] := key? It's just that for down to loops are not very kosher, I think. The question is almost philosophical, and are there algorithms better than those that I described?

function  InsertPos(var ar : arInt; key, pos : integer) : boolean;
var count, temp : integer;
begin
...
                count := Length(ar);  
                inc(count);
                 SetLength(ar, count);
...    
                 for j := pos to n-1 do
                                       begin
                                            temp := ar[j];
                                            ar[j] := key;
                                            key := temp;
                                       end;
...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2019-12-04
@dollar

The beauty of an array is that it allows you to search by indexes. And this is done very quickly, because this is an indexing in the memory itself. That is, the address of the memory cell is calculated using arithmetic operations:
BASE + [CELL SIZE] * OFFSET
And then a read / write access simply occurs.
Thus, in order to preserve this property of an array, it is necessary to store the contiguous structure in RAM. And for this, the operation of inserting into the middle of the arrays leads to a shift of half of the array by the size of one element. That is, it is copying a large piece of memory. This is bad.
In fact, it is neither bad nor good. If you rarely do this, and read the data often, then this is even excellent. But if the insertion occurs too often, then it is better to reconsider the chosen architecture of the application data structure. That is, to rewrite the program logic so that it does not have to be inserted into the middle of the array, this is usually possible.
And if this is not possible, then it may be that access by index is not needed, but only a complete enumeration (in the given order), then you can use a list, where the insertion is very fast. Well, and so on, depending on the specific task. The universality and speed of the algorithm, as a rule, contradict each other.

D
ddd329, 2019-12-04
@ddd329

Greetings!
If you answer directly to the question posed, then this is bad because, on average, half of the elements will have to be shifted.
If you need to maintain this array in a sorted form, then here you need not an algorithm, but another data structure - a binary (binary) tree. There you have a quick search and insert provided.
If you just need to implement such a feature as inserting into an array, then you definitely won’t do anything with shifting elements to the right, but increasing the length of the array every time is clearly a bad idea.
In the .Net Framework, an abstract data type (ATD) is implemented over an array - a list, i.e. List. So there, when creating an empty list, the array has a length of 4 elements, if it is necessary to insert the 5th element, then the length of the array simply increases by 2 times, and becomes equal to 8. If you insert the 9th element, then the size will increase to 16, and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question