algorithm strategies
algorithm strategies
There are various kinds of algorithm development techniques fornmulated and user
for different types of problems.
1.9.1 Iteration
Iteration is a process of repeating the same set of statements again and again untii
the specified condition holds true. Humans find iterative tasks boring but
computers
are very good at performing iterative tasks. Computers execute the
same set of
statements again and again by putting them in a loop.
In general, loops are classified as:
1. Counter-controlled Joops
2. Sentinel-controlled loops
i=i+ 1
The flowchart in
Figure illustrates the flow of control in the while structure that
structure.
corresponds to the preceding while
Yes
1K=100 Sum = sum+i
i=i+l
No
The flowchart clearly shows the repetition. The flow-line emerging from the
rectangle wraps back to the decision that is tested each timne through the loop until
the decision becomes false. Then the while structure exits and control passes to the
next statement in the program.
Recursion is a powerful
programming
problemns that can be expressed technique that can be used to solve the
in terms of similar
example. consider a problem to find the problems of snaller size Bo.
finding the factorial of n can be factorial of a number n. The problem of
size as n! = n x expressed in terms of a similar problem of smaller
(n-1)!. Recursion provides an
problems. elegant way of solving such
In recursive programming, a function calls
known as a recursive function, and itself. A function that calls itself is
the phenomenon is known as
recursion.
Recursion is classified according to the
following criteria:
1. Whether the
function calls itself directly (i.e., direct
(i.e.. indirect recursion). recursion) or indirectiy
2. Whether there is any
pend1ng operation on return from a
recursive call is the last operation of a function, recursive call. If the
the recursion is known as tail
recursIon.