*This preview shows
pages
1–3. Sign up to
view the full content.*

Introduction
Before we go any further into exploring algorithms that handle searching and
sorting you need to be sure that you can trace the execution of an algorithm
(code) in order to understand how the algorithm performs its operations.
The first
section of these notes deals with tracing code and showing a technique for doing
it thoroughly.
Tracing Code
How do you determine if an algorithm is correctly solving the problem for which it
was designed? How do you know that the decisions that algorithm makes are the
correct ones?
As an algorithm designer you must learn the art of code tracing.
In other words, you must act just like a computer and execute the algorithm in the
same step-by-step fashion by which the computer will execute the problem.
To
do this correctly, you cannot impose your knowledge of the problem or the design
of the algorithm on the execution process.
In other words, don’t try to
see
your
way to the solution or skip some of the steps that the algorithm will make
because you can
tell
what’s going to happen.
This is a sure-fire way to develop
an algorithm which will not be correct.
In order to properly trace the execution of
an algorithm you must act like a computer and on a sheet of paper draw a box for
each variable which is declared in the algorithm.
The set of boxes that you
construct will play the role of the computer memory in your trace.
As the values
in the boxes change due to the execution of statements in the algorithm you are
reflecting the changes of
state
that occur in the computer’s memory as the
algorithm executes.
Begin by executing the statements of the algorithm in
exactly the same manner that the computer would execute them and maintaining
the
state
of the memory by correcting changing the values of the variables in
their corresponding boxes.
At the end of the execution of the algorithm, the
values in the boxes will reflect the final state of the memory and thus the final
values in each of the variables.
As the trace is performed all I/O operations are
effected just as the computer would do.
Thus, read statements will consume
input values and make the corresponding assignments while print statements will
produce the output of the algorithm as it executes.
Below are the final set of
examples in this set of notes which give examples of tracing the execution of
algorithms.
Example 1
Searching and Sorting -
1
Searching and Sorting

This ** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*In the algorithms below the numbers preceding each line are for reference
purposes only and are
not
part of the algorithm.
1
void main ( ) {
2
int numbers [10];
3
int i, k, total;
4
//read in the numbers
5
for (i=0; i<=9; i++)
6
{
7
printf(“Enter your number:\n” );
8
scanf(“%d”, &numbers[ i ]);
9
}
10
//sum the numbers
11
total = 0;
12
for (k=0; k <=9; k++)
13
{
14
total = total + numbers[ k ];
15
}
16
printf(“The sum of your numbers is: %d
.\n“, total);
17 }
The trace
Suppose the input is:
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
Statements 1 through 4 are either comments or defining variables

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