Understanding Array
Operations
This presentation explores the fundamental operations of arrays: insertion,
deletion, and searching. We will delve into how these operations work and their
efficiency, considering how arrays are stored in contiguous memory.
preencoded.png
Insertion: Adding Elements to Arrays
Insertion involves adding a new element into an array at a specific position. Due to arrays being stored in a continuous block of memory,
simply "making space" in the middle is not possible.
When inserting, all elements from the target position onwards must be shifted one step to the right to free up the index for the new
element.
Insert New Element
Shift Elements
Place the new element into the now-
Determine Index
Move all subsequent elements one freed index.
Identify where the new element will be position to the right.
placed.
preencoded.png
Insertion Example
Step-by-Step Insertion
Let's illustrate with an example: inserting the value 25 at position 2 (index 1) into the array [10, 20, 30, 40, 50].
Initial Array
[10, 20, 30, 40, 50]
Shift Right
Elements from index 1 are shifted right, creating space: [10,
20, 20, 30, 40, 50]
Insert 25
The new element is placed at index 1: [10, 25, 20, 30, 40, 50]
The final array after insertion is [10, 25, 20, 30, 40, 50].
preencoded.png
Deletion: Removing Elements from Arrays
Deletion involves removing an element from a specific position. Unlike insertion, this doesn't require "freeing" memory in the middle.
Instead, once an element is identified for removal, all subsequent elements are shifted leftward to fill the gap. The last element is then
either discarded or left unused.
Identify Element Shift Left Adjust End
Locate the element to be removed by Move all elements to the right one The last element is either discarded or
its index. position to the left. becomes unused.
Example: Deleting the element at position 3 (index 2) from [10, 20, 30, 40, 50] results in [10, 20, 40, 50].
preencoded.png
Searching: Finding Elements in Arrays
Searching determines if an element exists in an array and, if so, its index. Two primary methods are Linear Search and Binary Search.
Linear Search Binary Search
• Starts at the first element and compares one by one. • Only works if the array is sorted.
• Works for both sorted and unsorted arrays. • Repeatedly divides the array into halves.
• Example: Searching for 40 in [10, 20, 30, 40, 50] involves • Much faster for large arrays.
comparing each element until 40 is found at index 3. • Example: Searching for 40 in [10, 20, 30, 40, 50] involves
checking the middle element (30), then searching the right
half, finding 40 at index 3.
preencoded.png
Key Takeaways
Insertion Deletion
Requires shifting elements to the Involves shifting elements to the
right to create space. left to fill gaps.
Searching
Linear search for any array, binary search for sorted arrays (faster).
Understanding these fundamental operations is crucial for efficient data
management and algorithm design when working with arrays.
preencoded.png