0
0
DSA Pythonprogramming

Array Insertion at Middle Index in DSA Python

Choose your learning style9 modes available
Mental Model
To insert a new value in the middle of an array, we shift all elements from that position to the right by one place to make space.
Analogy: Imagine a row of chairs with people sitting. To add a new person in the middle, everyone from that spot onward stands up and moves one chair to the right, freeing a chair in the middle.
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [5]
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], insert value 99 at index 2
Goal: Insert 99 at index 2 and shift elements right to keep order
Step 1: Start shifting elements from the last index to index 2 one step right
Index: 0   1   2   3   4   5
Array: [1] [2] [3] [4] [5] [5]
Why: We copy the last element to the next free spot to avoid overwriting
Step 2: Shift element at index 3 to index 4
Index: 0   1   2   3   4   5
Array: [1] [2] [3] [4] [4] [5]
Why: Continue shifting elements right to make space at index 2
Step 3: Shift element at index 2 to index 3
Index: 0   1   2   3   4   5
Array: [1] [2] [3] [3] [4] [5]
Why: Final shift to free index 2 for new value
Step 4: Insert value 99 at index 2
Index: 0   1   2   3   4   5
Array: [1] [2] [99] [3] [4] [5]
Why: Place new value in the freed spot
Result:
Index: 0   1   2   3   4   5
Array: [1] [2] [99] [3] [4] [5]
Annotated Code
DSA Python
class ArrayInsertion:
    def __init__(self, arr):
        self.arr = arr
        self.size = len(arr)

    def insert_at_middle(self, index, value):
        # Shift elements right from the end to index
        self.arr.append(0)  # Increase size by one
        for i in range(self.size, index, -1):
            self.arr[i] = self.arr[i - 1]  # Shift right
        self.arr[index] = value  # Insert new value
        self.size += 1

    def __str__(self):
        return ' -> '.join(str(x) for x in self.arr) + ' -> null'

# Driver code
array = [1, 2, 3, 4, 5]
obj = ArrayInsertion(array)
obj.insert_at_middle(2, 99)
print(obj)
self.arr.append(0) # Increase size by one
Expand array size by adding a placeholder at the end
for i in range(self.size, index, -1):
Loop backward from last element to index to shift elements right
self.arr[i] = self.arr[i - 1] # Shift right
Move element one position right to free space
self.arr[index] = value # Insert new value
Place the new value at the target index
self.size += 1
Update size to reflect new element
OutputSuccess
1 -> 2 -> 99 -> 3 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because in the worst case we shift half of the elements to the right
Space: O(1) because insertion is done in place with only one extra slot added
vs Alternative: Compared to creating a new array and copying all elements, this method is more space efficient but still requires shifting elements which takes linear time
Edge Cases
Insert at index 0 (start of array)
All elements shift right, new value placed at start
DSA Python
for i in range(self.size, index, -1):
Insert at last index (end of array)
No shifting needed, new value appended
DSA Python
for i in range(self.size, index, -1):
Insert into empty array
Array grows to size 1 with new value
DSA Python
self.arr.append(0)
When to Use This Pattern
When you need to add an element inside an array and keep order, think about shifting elements right to make space before insertion.
Common Mistakes
Mistake: Shifting elements left to right causing overwriting of data
Fix: Always shift elements from right to left (end to index) to avoid overwriting
Mistake: Not increasing array size before shifting
Fix: Append a placeholder to increase array size before shifting elements
Summary
It inserts a new value at a given middle index by shifting elements right to make space.
Use it when you want to add an element inside an array without losing existing data order.
The key is to shift elements from the end towards the insertion point to avoid overwriting.