➕ ARRAY INSERTION AT FRONT
Insert a new value at index 0 by shifting every existing element one position to the RIGHT
📥 Enter Array and New Value
🎬 Step-by-Step Animation
Enter an array and a new value, then press START INSERTION.
📊 Operation Statistics
Array Size
0
Shifts Completed
0
📝 Step-by-Step Explanation
🧮 Mathematical Representation
Suppose:
A = [10, 20, 30, 40, 50]
New value:
5
To insert at the FRONT, all existing elements must move one position RIGHT.
50 moves from index 4 to index 5.
40 moves from index 3 to index 4.
30 moves from index 2 to index 3.
20 moves from index 1 to index 2.
10 moves from index 0 to index 1.
Finally:
A[0] = 5
Final Array:
[5, 10, 20, 30, 40, 50]
A = [10, 20, 30, 40, 50]
New value:
5
To insert at the FRONT, all existing elements must move one position RIGHT.
A[5] ← A[4]
50 moves from index 4 to index 5.
A[4] ← A[3]
40 moves from index 3 to index 4.
A[3] ← A[2]
30 moves from index 2 to index 3.
A[2] ← A[1]
20 moves from index 1 to index 2.
A[1] ← A[0]
10 moves from index 0 to index 1.
Finally:
A[0] = 5
Final Array:
[5, 10, 20, 30, 40, 50]
💻 Algorithm
INSERT_FRONT(A,n,value)
Step 1: Start from the last element.
Step 2: Move each element one position RIGHT.
For: i = n − 1 down to 0
Perform: A[i + 1] = A[i]
Step 3: A[0] = value
Step 4: n = n + 1
Step 1: Start from the last element.
Step 2: Move each element one position RIGHT.
For: i = n − 1 down to 0
Perform: A[i + 1] = A[i]
Step 3: A[0] = value
Step 4: n = n + 1
⏱️ Time and Space Complexity
Time Complexity
O(n)
Number of Shifts
n
🎯 Final Result
Result will appear here.
No comments:
Post a Comment