A group of related data elements stored at adjacent memory locations is referred to as an array. One of the most basic data structures, it allows for random access to each data element by its index number.
Properties of an Array:
Arrays contain a number of crucial characteristics that make them helpful in a wide range of applications:
- Constant-time access: Regardless of the size of the array, accessing an element using its index takes a fixed amount of time since the elements of an array are stored in contiguous memory locations.
- Sequential storage is made simple by the fact that arrays keep their components in proximity to one another in memory.
- Fixed-size: An array’s fixed size refers to the fact that the maximum number of elements it may store is decided at the time of formation.
Representation of an array:
An array is typically represented as a block of contiguous memory locations, each of which stores an element of the array. Let us see an array declaration :
int arr[5]={1 , 2 , 3 , 4 , 5};
here,
- Name of the array: arr
- Type: int
- Size: 5, which means up to 5 elements can be stored in the array.
- Starting index: 0
Memory allocation of an array:
As mentioned earlier, an array’s data elements are all kept together in the main memory at contiguous locations. The base address, or the address of the first member in main memory, is represented by the array name. The array’s elements are each properly represented by an index. We can define the indexing of an array in the below ways –
- 0 (zero-based indexing): The first element of the array will be arr[0].
- 1 (one-based indexing): The first element of the array will be arr[1].
- n (n – based indexing): The first element of the array can reside at any random index number.
Basic Operations:
Following are the basic operations supported by an array.
Operation | Description |
Traverse | Iterating over every element in the array, this operation applies an operation to each element, such as printing its value. |
Insert | Adds a new element to the array at a specified index. |
Delete | Removes an element from the array at a specified index, shifting the remaining elements to fill the gap left by the deleted element. |
Update | This operation alters the value of an element at a certain index. |
Search | Searches for a specific element in the array either by its index or by its value. |
Complexity of Array operations:
Time and space complexity of various array operations are described in the following table.
Time Complexity
Operation | Average Case | Worst Case |
---|---|---|
Access | O(1) | O(1) |
Search | O(n) | O(n) |
Insertion | O(n) | O(n) |
Deletion | O(n) | O(n) |
Space Complexity
In array, space complexity for the worst case is O(n).
Note: also read about SQL: Sequence
Follow Me
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment