Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

algorithm strategies

Python

Uploaded by

pandi6382u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

algorithm strategies

Python

Uploaded by

pandi6382u
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

9 SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS

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

1.9.J.1 Counter-Controlled Loops

Counter-controlled looping is a form of l0oping in which the number of iterations to


be performcd is known in advance.
COunter-controlled loops are so named because
they use a control variabie, known as the loop counter, to
keep a track of loop
iterations. The cOunte-controlled loop starts with the initial value of the loop
COunter and teTIMinates when the finai value of the loop counter is
reached. Since
the cointer-controllcd ioops itcratc a fixcd number of times, which is
known in
advance, they are also known as icfínite repctition loops.
Computational Thinking and Problem Solving
33

Consider a program segment to find the sum of first l00 integers.


Example:
i=|
sum = 0
while (i<=100):
Sum # Sum +1

i=i+ 1

structure finishes executing, the sum will contain the desired


When the while
anSWer.

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.

1.9.1.2 Sentinel-Controlled Loops


hik
In sentinel-controlled looping, the number of times the iteration is to be performed
is not known beforehand. The execution or termination of the loop depends upon a
special value called the sentinel value. If the sentinel value is true, the loop body
wIn be executed, otherwise it will not. Since the number of times a loop will iterate
s not known in advance, this type of loop is also known as indefinite repetition
loop.
34
Problem Solving and Python Programming
Algorithm: To fnd the sum of first N integerS
1. Star
2. Read N

3. Assign sum =0,i=0


4. Calculate i =i+land sum = sum +1
5. Check whether
i>=N. if not repeat step 4.
6. Print the value of sum Otherwise go to next sten
7. Stop
1.9.2 Recursion

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.

A Function is directly recursive if it calls itself, i.e., the


explicit call itself. Indirect recursion occurs when function
to body contains an
a function calls another
function, which in turn cails another function,
function being called again. eventually resulting in the ornginal
Computational Thinking and Problem Solving
35

finding the factorial of a number


Recursive algorithm for
Example:
Step 1: Start
numbern
Step 2: Read
factorial (n)
Step 3: Call factorial f
Step 4: Print
Step 5: Stop
Factorial (n)

Step 1: ifn == Ithen return ]


Step 2: Else
f=n*factorial (n-1)
Step 3: Return f

You might also like