This document discusses the append operation for data structures. It describes:
1) The append operation adds an element to the end of a data structure. This is also called a push_back operation.
2) Currently, the push_back function creates a new, larger array, moves the old data to it, adds the new value, and uses the new array.
3) This approach is inefficient because it takes linear time for each push_back operation. With n elements, the total time would be quadratic, or O(n^2).
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 ratings0% found this document useful (0 votes)
8 views8 pages
02 Appending Operation
This document discusses the append operation for data structures. It describes:
1) The append operation adds an element to the end of a data structure. This is also called a push_back operation.
2) Currently, the push_back function creates a new, larger array, moves the old data to it, adds the new value, and uses the new array.
3) This approach is inefficient because it takes linear time for each push_back operation. With n elements, the total time would be quadratic, or O(n^2).
PhD from Simon Fraser University - Canada Bachelor / Msc from Cairo University - Egypt Ex-(Software Engineer / ICPC World Finalist) Get data ● 2 nice utilities might be to get the front or the back element ● But what if we want to: ○ Add in the back? ■ We call it append operation ■ Or push_back operation ○ Similarly insert in the front? ■ We call it push_front ● Take 10 minutes: ○ Create function void push_back(int value) ○ It adds an element to the current array push_back ● To add an element to an array, we need each time ○ Create new array ○ Move old data to it ○ Add new value ○ Use the new data and remove old one Usage ● One clear advantage now compare to primitive arrays, our data structure can grows normally in size! ● From efficiency perspective, what is wrong with our code? ● Take 10 minutes to think why?! ○ Hint: How many steps per push_back? ○ Hint: How many steps in all program? ○ Just approximate Approximately, How many steps ● Assume array has length size, If we tried to estimate the number of steps ○ ~ 5 x size + 7 ○ If size is 10^6, this is around 5 millions. ○ In other words, it takes linear number of steps to be finished
● For simplicity, let’s
assume it takes size steps ○ E.g. we dropped constants Approximately, What is Overall number of steps? ● Now this loop is iterating n = 10^6 step ○ We call push back, where size is increasing in each step, 1, 2, 3, …. n ○ So steps per a call are 1 + 2 + 3 + …..n ⇒ ~ n * (n+1) / 2 = 1/2 (n^2 + n) steps ○ For simplicity, let’s keep only the largest factor here ⇒ n^2 ■ Drop constant ½ and the smaller factor n ○ In other words, it takes quadratic number of steps for loop/body to be finished ○ So for n=10^6, we take ~ 10^12 (multiplied by some factor, e.g. 5) ● Now, you know, mathematically, why this code takes too much time! Your turn ● In the next video, we will present a simple idea that speed the code ● Take 15 minutes to guess the trick ● Also, consider today analysis one good reason why we need to study the data structures NOT just use them! “Acquire knowledge and impart it to the people.”