The document discusses analyzing the runtime of an algorithm that performs addition operations on a list. It determines that adding N items to a list using this algorithm results in a quadratic runtime of O(N^2) because each addition requires shifting the remaining items over by one index.
The document discusses analyzing the runtime of an algorithm that performs addition operations on a list. It determines that adding N items to a list using this algorithm results in a quadratic runtime of O(N^2) because each addition requires shifting the remaining items over by one index.
The document discusses analyzing the runtime of an algorithm that performs addition operations on a list. It determines that adding N items to a list using this algorithm results in a quadratic runtime of O(N^2) because each addition requires shifting the remaining items over by one index.
The document discusses analyzing the runtime of an algorithm that performs addition operations on a list. It determines that adding N items to a list using this algorithm results in a quadratic runtime of O(N^2) because each addition requires shifting the remaining items over by one index.
operations for each call to add is (because it has to move every ite item at position 0).
For the N ca time
for all N calls? It is proport initial array is large enough to hold N items, the is proportional to the number of items in the li item already in the array one place to the right calls shown above, the list lengths are: 0, 1, 2... ortional to 0 + 1 + 2 + ... + N-1. Recall that we don't care about t method createList, the "problem different for different values of N method createList) is not indepe then the number of list when add is called ht to make room for the new ... N-1. So what is the total t the exact time, just how the time depends on t em size" is the value of N (because the number f N). It is clear that the time for the N calls (and endent of N (so createList is not a constant-tim n the problem size. For er of operations will be and therefore the time for time method). Is it 23 Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 24 proportional to N (linear in N)? operations performed by createL different values of N: Clearly, the value of the sum do not linear in N. In the following of the N calls. The value of the sum (0+1+2+... that the bars fill about half of the therefore, the sum of the areas o method createList is proportiona number of operations will quadr problem size. Notes (Big O) We express complexity using bi • A constant-time method • A linear-time method is on 015-2016 )? That would mean that doubling N would dou teList. Here's a table showing the value of 0+1+ N 0+1+2+...+(N- 1) 4 6 8 28 16 120 does more than double when the value of N d ng graph, the bars represent the lengths of the li ...+ (N-1)) is the sum of the areas of the individ the square. The whole square is an N-by- N squ s of the bars is about (N2)/2. In other words, the nal to the square of the problem size; if the pro druple. We say that the worst-case time for cre big-O notation. For a problem of size N: d is "order 1": O(1) is "order N": O(N) ouble the number of 1+2+...+ (N-1) for some doubles, so createList is list (0, 1, 2... N-1) for each vidual bars. You can see quare, so its area is N2; he time for roblem size doubles, the reateList is quadratic in the Cairo University Faculty of Computers and Information CS214-Data Structures, 2nd Term, 201 on 015-2016 • A quadratic-time method od is "order N squared": O(N2) Note that the big-O expressions gets large enough, constants and than a linear-time method, which ns do not have constants or low-order terms. Th nd low-order terms don't matter (a constant-tim ich will be faster than a quadratic-time method Formal definition: A function T than some value n0: This is because; when N ime method will be faster od). T (N) is O (F (N)) if for some constant c and d for all values of N greater T (N) <= c * F (N) The idea is that T(N) is the exac size N, and that F(N) is an upper a problem of size N will be no w the least upper bound on the actu act complexity of a method or algorithm as a fu per-bound on that complexity (i.e., the actual tim worse than F(N)). In practice, we want the sm ctual complexity. For example, consider T (N) = 3 n0 = 2. This is because for all va function of the problem time/space or whatever for mallest F (N) -- 3 * N2 + 5. We can show that T (N) is O (N2) values of N greater than 2: ) by choosing c = 4 and 3 * N2 + 5 <= 4 * N2 T(N) is not O(N), because whate N greater than n0 so that 3 * N2 atever constant c and value n0 you choose, I ca + 5 is greater than c * N. can always find a value of PROBLEM (4) Objective How to determine the ru kinds of statements are used. running time of a piece of code? You will find d that it depends on what Problem Statement Measure the complexity are simple unless noted otherwis nts: (assume the statements