tldr An unsorted array is analogous to a set. Like a set, elements can be added and removed, iterated over, and read. But, as with a set, it makes no sense to talk about inserting an element at a specific position, because to do so would be an attempt to impose a sorting order on what is, by definition, unsorted.
According to academic literature for arrays it is constant O(1) and for Linked Lists it is linear O(n).
It is worth understanding why the academic literature quotes array insert as O(1) for an array. There are several concepts to understand:
An array is defined as being unsorted (unless explicitly stated otherwise).
The length of an array, defined as the number of elements that the array contains, can be increased or decreased arbitrarily in O(1) time and there is no limit on the maximum size of an array.
(In a real computer this is not be the case, due to various factors such as memory size, virtual memory, swap space, etc. But for the purpose of algorithm asymptotic analysis these factors are not important - we care about how the running time of the algorithm changes as the input size increases towards infinity, not how it performs on a particular computer with a particular memory size and operating system.)
Insert and delete are O(1) because the array is an unsorted data structure.
Insert is not assignment
Consider what it actually means to add an element to an unsorted data structure. Since there is no defined sorting order, whatever order actually occurs is arbitrary and does not matter. If you think in terms of an object oriented API, the method signature would be something like:
Array.insert(Element e)
Note that this is the same as the insert methods for other data structures, like a linked list or sorted array:
LinkedList.insert(Element e)
SortedArray.insert(Element e)
In all of these cases, the caller of the insert method does not specify where the value being inserted ends up being stored - it is an internal detail of the data structure. Furthermore, it makes no sense for the caller to try and insert an element at a specific location in the data structure - either for a sorted or unsorted data structure. For an (unsorted) linked list, the list is by definition unsorted and therefore the sort order is irrelevant. For a sorted array, the insert operation will, by definition, insert an element at a specific point of the array.
Thus it makes no sense to define an array insert operation as:
Array.insert(Element e, Index p)
With such a definition, the caller would override an internal property of the data structure and impose an ordering constraint on an unsorted array - a constraint that does not exist in the definition of the array, because an array is unsorted.
Why does this misconception occur with arrays and not other data structures? Probably because programmers are used to dealing with arrays using the assignment operator:
array[0] = 10
array[1] = 20
The assignment operator gives the values of an array an explicit order. The important thing to note here is that assignment is not the same as insert:
- insert : store the given value in the data structure without modifying existing elements.
- insert in unsorted : store the given value in the data structure without modifying existing elements and the retrieval order is not important.
- insert in sorted : store the given value in the data structure without modifying existing elements and the retrieval order is important.
- assign a[x] = v : overwrite the existing data in location x with the given value v.
An unsorted array has no sort order, and hence insert does not need to allow overriding of the position. insert is not the same thing as assignment. Array insert is simply defined as:
Array.insert(v):
array.length = array.length + 1
// in standard algorithmic notation, arrays are defined from 1..n not 0..n-1
array[array.length] = v
Which is O(1).