Professional Documents
Culture Documents
index 0 1 2 3 4 5 6 7 8
test_score 89 79 88 100 90 56 85 40 10
Recall on Stacks
Get ball 1, how?
3
Remove ball 3.
Remove ball 2.
2
1
Seatwork: Tower of Hanoi
1
2
3
4
2 5 6 8 10 12 15 18 20 21
10
5 18
2 6 12 20
8 15 21
What is an algorithm?
• An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of time.
• It is a step-by-step procedure in solving a problem in a
finite amount of time.
Experimental Studies
• Write a program
implementing the algorithm
• Run the program with inputs
of varying size and
composition.
• Use a function (like a built-in
clock() function), to get an
accurate measure of the
actual running time
• Plot the results
Limitations of Experiments
• It is necessary to implement the algorithm,
which may be difficult
• Result may not be indicative of the running
time on other inputs not included in the
experiment
• In order to compare two algorithms, the
same hardware and software
environments must be used.
Algorithm
• Can be represented various forms
• Unambiguity / clearness
• Effectiveness
• Finiteness / termination
• Correctness
Following an Algorithm
Developing an Algorithm
• There are two methodologies used to
develop computer solution to a problem
– Top-Down Design focuses on the tasks to be
done
– Object-Oriented Design focuses on the data
involved in the solution
Top Down Design
• Top-Down Design
– Problem solving technique in which the problem is
divided into subproblems; process is applied to each
subproblem.
• Modules
– Self-contained collection of steps, that solve a problem
or subproblem.
• Abstract Step
– An algorithmic step containing unspecified details
• Concrete Step
– An algorithm step in which all details are specified
Top Down Design
Example: Planning a Large Party
Testing the Algorithm
Important distinction
algorithm
Algorithmic Solution
(different from a conventional solution)
Example of Computation Problem: Sorting
22 17 36 16 43
Bubble Sort Illustrated
Pass 2:
[0] [1] [2] [3] [4] Operation
Step 1: 22 17 36 16 43 Compare [0] and [1]. Swap
17 22 16 36 43
Bubble Sort Illustrated
Pass 3:
[0] [1] [2] [3] [4] Operation
Step 1: 17 22 16 36 43 Compare [0] and [1]. No Swap
17 16 22 36 43
Bubble Sort Illustrated
Pass 4:
[0] [1] [2] [3] [4] Operation
Step 1: 17 16 22 36 43 Compare [0] and [1]. Swap
16 17 22 36 43 Sorted Array
Values from each pass
int arraybubble[] = {43, 22, 17, 36, 16};
Pass 1: 22 17 36 16 43
Pass 2: 17 22 16 36 43
Pass 3: 17 16 22 36 43
35 15 18 28 34 4 19 45 63 23