Loops and Timecomplexity
Loops and Timecomplexity
DEFINITION
An algorithm is a finite sequence of precise instructions for performing a
computation or for solving a problem.
Algorithms
Example 1 Finding the Maximum Element in a Finite Sequence.
procedure
for
if
return
Algorithms
Properties Of Algorithms: There are several properties that algorithms generally
share. They are useful to keep in mind when algorithms are described. These properties are:
▶ Input. An algorithm has input values from a specified set.
▶ Output. From each set of input values an algorithm produces output values from a specified set. The
output values are the solution to the problem.
▶ Definiteness. The steps of an algorithm must be defined precisely.
▶ Correctness. An algorithm should produce the correct output values for each set of input values.
▶ Finiteness. An algorithm should produce the desired output after a finite (but perhaps
large) number of steps for any input in the set.
▶ Effectiveness. It must be possible to perform each step of an algorithm exactly and in a
finite amount of time.
▶ Generality. The procedure should be applicable for all problems of the desired form, not
just for a particular set of input values.
Loops
Repetition statements allow us to execute a statement or a block of
statements multiple times
Often they are referred to as loops
Like conditional statements, they are controlled by boolean
expressions
There usually three kinds of repetition statements:
i. while
ii. for
iii. do
The programmer should choose the right kind of loop statement for
the situation
Loops
There are types of loops according to their applications
Loop
Single Nested
Independent Dependent
The Statement
A while statement has the following syntax:
while ( condition )
statement;
If the condition is true, the statement is executed
Then the condition is evaluated again, and if it is still true, the
statement is executed again
The statement is executed repeatedly until the condition becomes
false
The Statement
condition
evaluated
true false
statement
The Statement
A for statement has the following syntax:
initialization: This is the first component, which is used for the initialization of the
counter variable which is used to control the loop. This statement is only executed
once.
condition: It is a boolean expression, which is evaluated before each iteration of the
loop. If the boolean expression is true, the loop body is executed.
update: This statement is executed after each iteration of the loop. This statement
updates the counter variable or other values that are present in the loop.
The Statement
condition
statement
The Statement
A do while statement has the following syntax:
do {
// body of do-while loop
} while (condition);
the body of the loop is executed first and then the test condition/expression is
checked, unlike other loops where the test condition is checked first.
When the test condition is evaluated as true, the program control goes to the start of
the loop and the body is executed once more.
The above process repeats till the test condition is true.
When the test condition is evaluated as false, the program controls move on to the
next statements after the do…while loop.
The Statement
condition
true false
Time Complexity
The time complexity of an algorithm can be expressed in terms of the number
of operations used by the algorithm when the input has a particular size. The
operations used to measure time complexity can be the comparison of
integers, the addition of integers, the multiplication of integers, the division of
integers, or any other basic operation.
DEFINITION
Let and be functions from the set of integers or the set of real numbers to the set of
real numbers. We say that is if there are constants and such that
whenever . [This is read as “ is big-oh of .”]
Intuitively, the definition that is says that grows slower than some fixed multiple of
as grows without bound.
Big- Notation
Example 1 How can big- notation be used to estimate the sum of the first
positive integers?
Solution: Because each of the integers in the sum of the first positive integers
does not exceed , it follows that
Solution: A big- estimate for can be obtained by noting that each term in the
product does not exceed . Hence,
We, in this section only find the time complexity of a loop with no in body
statements.
Time Complexity Of Loops
We find the time complexity of a loop in six steps
I- Dry Run:
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
II- Generative Function: Now from the results of iterations find the
sequence. In this example there is a constant difference of 4, which means
the sequence is
Time Complexity Of Ascending Loops
If If
Or we say
Time Complexity Of Ascending Loops
Range : Range :
Time Complexity Of Ascending Loops
Count of : Count of :
Time Complexity Of Ascending Loops
Big : Big
Time Complexity Of Ascending Loops
Generic form:
I- Dry Run:
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃 ⋯
II- Generative Function: Now from the results of iterations find the
sequence. In this example there is a constant addition of , which means the
sequence is
Time Complexity Of Ascending Loops
If If
Or we say
Time Complexity Of Ascending Loops
Range : Range :
Time Complexity Of Ascending Loops
Count of : Count of :
Time Complexity Of Ascending Loops
Big : Big
Time Complexity Of Descending Loops
I- Dry Run:
𝒏 𝒏− 𝟒𝒏− 𝟖𝒏− 𝟏𝟐 ⋯ 𝒏− 𝟒 𝒌
II- Generative Function: Now from the results of iterations find the
descending sequence. In this example there is a constant subtraction of 4,
which means the sequence is
Time Complexity Of Descending Loops
If If
Or we say
Time Complexity Of Descending Loops
Range : Range :
Time Complexity Of Descending Loops
Count of : Count of :
Time Complexity Of Descending Loops
Big : Big
Time Complexity Of Descending Loops
Generic form:
I- Dry Run:
𝒏 𝒏− 𝒃𝒏− 𝟐 𝒃 ⋯
II- Generative Function: Now from the results of iterations find the
sequence. In this example there is a constant difference of , which means
the sequence is
Time Complexity Of Descending Loops
If If
Or we say
Time Complexity Of Descending Loops
Range : Range :
Time Complexity Of Descending Loops
Count of : Count of :
Time Complexity Of Descending Loops
Big : Big
Time Complexity Of Nested Loops (Ind)
Terminates
I- Dry Run:
𝒏
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
𝒏− 𝟒
Outer loop 𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌 Inner loop
…
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
𝒏− 𝟒 𝒌
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
Time Complexity Of Nested Loops (Ind)
… Function of
Inner loop
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
𝒏− 𝟒 𝒌
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
Time Complexity Of Nested Loops (Ind)
Or we say Or we say
Time Complexity Of Nested Loops (Ind)
I- Dry Run:
𝒏− 𝒃
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯ 𝒂+𝒌𝒃
𝒏− 𝟐 𝒃
Outer loop 𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯ Inner loop
…
𝒂 𝒂+𝟑 𝒃 ⋯ 𝒂+𝒌𝒃
𝒏− 𝒌𝒃
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯
Time Complexity Of Nested Loops (Ind)
Generic form
… Function of
Inner loop
𝒂 𝒂+𝟑 𝒃 ⋯ 𝒂+𝒌𝒃
𝒏− 𝒌𝒃
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯
Time Complexity Of Nested Loops (Ind)
Generic form
Or we say Or we say
Time Complexity Of Nested Loops (Ind)
Generic form