Script 1 2
Script 1 2
Script 1 2
ALGORITHM DESIGN
Dear friends,
Algorithm
Algorithm design
Algorithm 1:
Making Coffee –
This algorithm will describe how to make coffee
Step 1: Start
Step 2: Get a teapot, whose size depends on, for many
people you are making the coffee for.
Step 3: Pour water into the pot until the pot is almost filled
with water.
Step 4: Put the pot on the stove and turn the stove on.
Step 5: Let the water in the pot boil for five minutes.
Step 6: Get the coffee powder, sugars and milk as you like
and put them into the boiling pot.
Step 7: Now boil for another one minute.
Step 8: Turn off the stove and remove the pot from the
stove.
Step 9: Now the coffee is ready, enjoy it (stop).
Let us consider another example of algorithm to check
whether the given number is even or odd.
Algorithm 2:
Find even or odd - this algorithm will find whether a given
number is even or odd.
num is the input number, rem is the reminder of a given
number.
Step 1: start
Step 2: input num
Step 3: rem=num mod 2
Step 4: if the rem=0 then
“Given num is even"
Other wise
"Given num is odd"
Step 5: stop
7. Analyzing algorithms:
The idea behind a top down design is to first divide the prob-
lem into a small number of broad subtasks. We call the initial
level as top level, main module, or level 0. The number of
subtasks at this point should be small, as shown in the fig-
ure. For most programs, we would expect three to seven
subtasks at this level. The subtasks of each level can be
again subdivided into smaller modules by repeating the
process of division. Most often, for each of the subtasks at
level 0, we will start a new module at level 1.
Algorithm 3:
Linear Search
A linear array DATA with N elements and specific ITEM of
information are given. This algorithms finds the location LOC
Step 1: [initialize] set k=1 and LOC: =0
Step 2: Repeat step 3and 4 while LOC: =k and k<=N
Step 3: IF ITEM=DATA[k], Then: Set LOC: =k
Step4: Set K=k+1[increment counter]
[End of Step 2 Loop]
Step 5: [Successful?]
IF LOC=0, then
Write: ITEM is not in the array DATA
ELSE
Write: LOC is the location of ITEM
[End of if structure]
Step 6: Exit
Algorithm 4:
Binary Search
BINARY (DATA, LB, UB, ITEM, LOC)
Here DATA is a stored array with lower bound
LB and upper bound UB, and ITEM is a given item of
information. The Variables BEG, END and MID denote,
respectively, the beginning, end and middle location of a
segment of elements of DATA. This algorithm find the
location LOC of ITEM in DATA or sets LOC=NULL.
Step 1: [Initialize segment variables.]
Set BEG=LB, END = UB and MID =
INT((BEG+END)/2).
Step 2: Repeat Step 3 and 4 while BEG<= END and
DATA[MID] ≠ ITEM
Step 3: if ITEM < DATA[MID], then:
Set END=MID-1.
Else:
Set BEG = MID+1.
Step 4: Set MID = INT ((BEG+END)/2)
Step 5: If DATA[MID] = ITEM then:
Set LOC = MID
Else:
Set LOC =NULL
Step 6: Stop.
……………………………………………………………………
……………………..
.
……………………………………………………………………
……………………..
.
……………………………………………………………………
……………………..
Step N-1: Compare A[1] with A[2] and arrange them so that
A[1]<A[2].
[After n-1 steps, the list will be stored in increasing order.]
Summary
Algorithm is a sequential solution for any program
and which is written in human language or natural language.
Algorithm is the basic way or approach to easily handle a
given problem. After implementing algorithm for a given
problem it can be easily converted into computer programs
on any programming language. The next section describes
the top down approach of problem solving. The idea behind
a top down design is to first divide the problem into a small
number of broad subtasks. At the end of this module we
discussed some basic algorithms for searching and sorting,
and specifically we studied the algorithms for linear search,
binary search and the bubble sort.
Assignments
Reference