Lecture 4
Lecture 4
Array
Lecture 4
Introduction
Array is a collection of items of the same variable type that are stored at
contiguous memory locations. It is one of the most popular and simple
data structures used in programming.
Basic terminologies of Array
Result:
[0, 0, 0, 0, 0]
Types of Array (On the basis of Size)
Dynamic Sized Arrays:
# Dynamic Array
arr = []
Types of Array (on the basis of Dimensions)
One-dimensional Array(1-D Array):
Types of Array (on the basis of Dimensions)
Multi-dimensional Array(2-D Array):
Operations on Array
1. Array Traversal:
The process of accessing and processing each element of an array
sequentially.
For example,
arr = [10, 20, 30, 40, 50]
Here:
•The first element (10) is at index 0.
•The second element (20) is at index 1.
•The last element (50) is at index 4.
Operations on Array
1. Array Traversal:
The process of accessing and processing each element of an array
sequentially.
For example,
arr = [10, 20, 30, 40, 50]
Here:
•The first element (10) is at index 0.
•The second element (20) is at index 1.
•The last element (50) is at index 4.
Operations on Array
2. Insertion in Array:
The process of adding a new element at a specific position while
maintaining the order of the existing elements.
Arrays are stored in contiguous memory locations, meaning elements are arranged
in a sequential block. When inserting a new element, the following happens:
• Identify the Position: Determine where the new element should be inserted.
• Shift Elements: Move the existing elements one position forward to create space
for the new element.
• Insert the New Element: Place the new value in the correct position.
• Update the Size (if applicable): If the array is dynamic, its size is increased.
Operations on Array
3. Deletion in Array:
The process of removing an element from a specific position while
maintaining the order of the remaining elements.
Since arrays have contiguous memory allocation, deleting an element does not
reduce the allocated memory size. Instead, it involves: