2 Notion of Algorithm.docx

# In general an algorithm that has time complexity o n

• 19

This preview shows pages 14–17. Sign up to view the full content.

In general, an algorithm that has time complexity O (n 2 ) will run as a program in time O (n 2 ), no matter what language or machine is chosen for the implementation of the algorithm. To analyze an algorithm, we make some simplifying assumption: (1) First assumption: each statement , except perhaps for procedure and function calls, takes the same unit amount of time . This will not affect the timing analysis since we are only interested in big-O estimates and so are ignoring constant multiples. As an example, if multiplication takes five times as long as addition when implemented on a computer, we are off in our timing estimate by no more than a factor of five if we treat them as equal in the algorithm, and the multiple of five will be absorbed in our big-O estimate anyway. The instructions not obeying to this constraint are: IF instruction. Repetitive instruction sequences (loops). Functions and procedures calls. Ignoring for the moment the function and procedure calls, there are two more simplifying assumptions we will make in the timing analysis of an algorithm: (2) Unless there is compelling reason to the contrary, we will always assume that an if statement takes time equal to the larger of its two branches (in order to obtain worst-case timing estimates). (3) Unless there is compelling reason to the contrary, we will always assume that the statements within a loop will be executed as many times as the maximum permitted by the loop control.

This preview has intentionally blurred sections. Sign up to view the full version.

------------------------------------------------------------ Example 2.4.a. Consider the following routine: (1) Searches an array with n elements for any element with value equal to key. (2) Returns the last location where key is found in variable where, or zero if not found. [2.4.a]. ------------------------------------------------------------ PROCEDURE LinearSearch (n,key: integer, a: NumberArray; VAR where: integer); {Searches an array with n elements for any element with value equal to key. Returns the last location where key is found in variable where, or zero if not found.} VAR index: integer; BEGIN where: = 0 FOR index:= 1 TO n DO IF a[index]= key THEN [2.4.a] where:= index END ; { LinearSearch } ------------------------------------------------------------ Entering procedure and initializing O (1) variable where FOR loop ( n iterations) O (1+n+1) = O (n) IF statement and O (1) O (n*1) = O (n) its branch Return from procedure O (1) Fig. 2.4.a. Temporal scheme of the algorithm [2.4.a] The figure 2.4.a shows the temporal scheme of the algorithm and the corresponding O-big estimation. We have included timing for operations, such as entering and leaving the procedure , that are not explicitly written as part of the routine. In the future we will omit these from our timing analysis, since they contribute only a constant amount of additional time, and so will not appear in the big-O estimate. Notice that we have made use of the rules for evaluating big-O expressions.
Example 2.4.b .

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern