0% found this document useful (0 votes)
42 views

04 Insertion

The document discusses insertion into an array data structure. It explains that to insert a value into a specific index, all subsequent values must be shifted over by one index to make room. This shifting operation takes linear time (O(n)) in the worst case when inserting at the start of the array, as the entire array must be shifted. Therefore, frequent insertion should be avoided for arrays to maintain efficient performance.

Uploaded by

Ramsha Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

04 Insertion

The document discusses insertion into an array data structure. It explains that to insert a value into a specific index, all subsequent values must be shifted over by one index to make room. This shifting operation takes linear time (O(n)) in the worst case when inserting at the start of the array, as the entire array must be shifted. Therefore, frequent insertion should be avoided for arrays to maintain efficient performance.

Uploaded by

Ramsha Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structures

Insertion

Mostafa S. Ibrahim
Teaching, Training and Coaching since more than a decade!

Artificial Intelligence & Computer Vision Researcher


PhD from Simon Fraser University - Canada
Bachelor / Msc from Cairo University - Egypt
Ex-(Software Engineer / ICPC World Finalist)
Insertion operation
● Another critical feature is insertion Index 0 1 2 3 4

● Let’s say we have the array on right values 10 8 7 5 3


○ {10, 8, 7, 5, 3}
● Now we want to insert value 17 in index 2
○ New array: {10, 8, 17, 7, 5, 3}
● How to do it? Think for 5 minutes
Index 0 1 2 3 4 5

values 10 8 17 7 5 3
Insertion operation
● {10, 8, 7, 5, 3} Index 0 1 2 3 4

● First, we need to shift all values from index 2 to values 10 8 7 5 3

the right side


● {10, 8, EMPTY, 7, 5, 3}
● Then add the value in the requested position
● {10, 8, 17, 7, 5, 3}
Index 0 1 2 3 4 5
● Take 15 minutes and try to implement
values 10 8 17 7 5 3
Insertion
Efficiency
● How fast is our insertion?
● If we inserted at position idx, we need first to shift the elements to the right
● Let number of shiftings k = size - idx ⇒ we need 3k+1 steps
● Then we need to put the element and increase the size : 2 steps
● Total 3k + 2. Let’s drop constants ⇒ K
● What is important to measure efficiency is when K is large
● This happens when idx = 0 (shift whole array) ⇒ size steps
● Overall: we need linear number of steps in the worst case (size)
● This means we need to be careful and not call this function a lot!
“Acquire knowledge and impart it to the people.”

“Seek knowledge from the Cradle to the Grave.”

You might also like