0% found this document useful (0 votes)
5 views

Algorithm design

The document explains the concept and creation of trace tables, which are used to document the execution of algorithms for debugging and understanding code behavior. It also describes the linear search algorithm, detailing its steps and providing examples of its implementation in Python. Trace tables are beneficial for visualizing algorithms, identifying errors, and documenting complex code.

Uploaded by

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

Algorithm design

The document explains the concept and creation of trace tables, which are used to document the execution of algorithms for debugging and understanding code behavior. It also describes the linear search algorithm, detailing its steps and providing examples of its implementation in Python. Trace tables are beneficial for visualizing algorithms, identifying errors, and documenting complex code.

Uploaded by

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

Trace Table

• A trace table is a tool used to document the step-by-step execution (dry


run) of an algorithm or program. It helps track changes to the variables
and shows how an algorithm works over time. This is especially useful for
debugging or understanding the behavior of the code.
• A trace table is set up with a column for each variable and a column for any
output
How to Create a Trace Table
Identify Variables : List all the variables that appear in the algorithm.
Create Columns : Each variable should have its own column in the trace table, and
there should be additional columns for input/output if needed.
Document Each Step : As the algorithm executes, update the values of the
variables in the table at each step.
An example of trace
table is given : The
flowchart below
inputs the height of
children who want to
ride on a
rollercoaster. Children
under 1.2 meters are
rejected. The ride
starts when eight
children have been
accepted.
Benefits of Trace Tables
Understand the Algorithm: They help visualize how an algorithm behaves for
given input data.
Debugging: Trace tables can help identify logic errors by showing where the
algorithm deviates from expected behavior.
Documentation: Useful for explaining algorithms in educational settings or
documenting complex code.
When to Use Trace Tables
• During code reviews or debugging.
• When explaining algorithms in computer science classes.
• For analyzing the correctness of an algorithm.
Linear Search

Linear search is a simple searching algorithm used to find the position of a


target value (or key) within a list. It is basic method for searching through
an array or list.

How Linear Search Works

1.Start at the beginning of the list.


2.Compare the current element with the target value.
3.If the current element matches the target, return its position (index).
4.If the current element does not match, move to the next element.
5.Repeat steps 2-4 until the target is found or the end of the list is reached.
6.If the end of the list is reached without finding the target, return a message indicating that
the target is not in the list.
StudentName = ["Alice", "Bob", "Charlie", "David", "Eve"]
ClassSize = len(StudentName)

Name = input("Please enter name to find: ")

Found = False
Counter = 0

while not Found and Counter < ClassSize:


if Name == StudentName[Counter]:
Found = True
else:
Counter += 1

if Found:
print(f"{Name} found at position {Counter + 1} in the list.") # Position is Counter + 1 for 1-based index
else:
print(f"{Name} not found.")
while True:

Name = input("Please enter name to find (or type 'exit' to stop): ")

if Name.lower() == 'exit’:

print("Exiting the program.") break


def linear_search(arr, target):
for index in range(len(arr)):
if arr[index] == target:
return index

return -1

numbers = [3, 8, 12, 5, 7, 18]


target_value = int(input("Enter the number to search for: "))

result = linear_search(numbers, target_value)

if result != -1:
print(f"Target found at index: {result}")
else:
print("Target not found in the list.")

You might also like