Junior — Middle
What is the time complexity of insertion, search, read, and delete operations in an array in the worst and average cases?
sobes.tech AI
Answer from AI
For a standard array in iOS (e.g., NSArray or NSMutableArray), the approximate characteristics of operations are as follows:
-
Insertion:
- At the end of the array (append): average and worst case — O(1) amortized (if no reallocation is required).
- At the beginning or middle: O(n), as elements need to be shifted.
-
Search (if searching by index): O(1), since the array is indexed.
-
Reading by index: O(1).
-
Deletion:
- From the end of the array: O(1).
- From the beginning or middle: O(n), due to shifting elements.
Thus, insertion and deletion operations in the middle or beginning of the array have linear complexity, while access and insertion/deletion at the end are constant (amortized).