Download Chapter 8: Algorithm

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Big O notation wikipedia , lookup

Addition wikipedia , lookup

Halting problem wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Algorithm characterizations wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
BUILDING
A
PROGRAM
1
Building
a program
 The steps to
building a
program
include
writing,
editing,
compiling,
and linking
code.
2
PROGRAM
EXECUTION
3
Program execution
4
Algorithms + Data Structures
= Programs
- Niklaus Wirth, 1975.
5
CONCEPT
6
Informal definition
 Informally, an algorithm is a step-by-step method
for solving a problem or doing a task.
 An algorithm accepts an input list of data and
creates an output list of data.
7
THREE CONSTRUCTS
8
Three constructs
 A program is a combination of sequence constructs,
decision constructs, and repetition constructs.
9
Flowcharts for three constructs
 A flowchart is a pictorial representation of an
algorithm.
10
Appendix : Flowcharts
SYMBOL
n
NAME
APPLICATION
Terminal
Shows the beginning or
end of an algorithm
Flow Lines
Show the action order
in an algorithm
Connector
Shows the continuity of
the algorithm on the next
page
11
START and STOP
START
STOP
12
Connectors
START
1
1
STOP
13
Sequence Symbols
Assignment statement
Input/output statement
Module call
Compound statement
14
Assignment statement
variable ← expression
15
Module-Call Statement
START
AVRG
AVRG
RETURN
STOP
16
Two-Way Selection
F
False Statement
Condition
T
True Statement
17
for Loop
End of Initialization
loop
action Limit test
F
T
18
Example 1
Write an algorithm that finds the average of
two numbers
19
Algorithm 1:
Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result of Step 2
End
START
Input X, Y
Sum = X + Y
Avg = Sum / 2
Output Avg
End
20
Example 2
Write an algorithm to find the largest of
1000 numbers.
21
Flow Chart
START
Input 1000 integers
Largest = 0
i=0
While i < 1000
F
T
integer[i] > Largest
F
T
Largest = integer[i]
i = i + 1
Output Largest
End
22
Algorithm 2:
1.
2.
3.
4.
Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
Set Largest to 0
Set Counter to 0
while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
Return Largest
End
23
Example 3: Prime Number Test

Given a natural number N, where N > 1.


If there exists an integer i, 1 < i < N, such that i
can evenly divide N, then N is a composite
number.
Otherwise, N is a prime number.
24
START
N
isPrime = true
i=i+1
i=2
i<N
i evenly
divide N
Y isPrime = false
N
25
Y
isPrime
N is a prime
number
N
N is NOT a
prime number
END
26
Conditional Operator

You may implement the above structure as


if (isPrime)
cout << n << " is a prime number." << endl;
else
cout << n << " is NOT a prime number." << endl;
or with a conditional operator

cout << N << " is" << ( (isPrime)?" ":" NOT " )
<< "a prime number." << endl;
27
SUB-ALGORITHMS
28
Concept of a subalgorithm
 An algorithm can be broken into smaller units called
subalgorithms.
START
gcd(a, b)
X, Y
i=gcd(X,Y)
.
.
.
i=1
“Greatest common
divider = “, i
“They are
mutual prime.”
RETURN
END
29
BASIC
ALGORITHMS
30
Summation
31
Bubble sort
32
Example of bubble sort
8
45
33
Example of bubble sort
8
78
8 45
34
Example of bubble sort
8 23
8 78
8 45
The smallest number
is moved to the head.
35
Example of bubble sort
32 45
36
Example of bubble sort
32 78
32 45
37
Example of bubble sort
32 78
32 45
The second smallest
number
38
Example of bubble sort
39
Example of bubble sort
And so on …
40
Search concept
 Searching, a process to locate a target in a list of
data, is a basic algorithm.
 Sequential search is used for unordered lists.
 Binary search is used for ordered lists.
41
Example of a sequential search
42
Example of a sequential search
43
Example of
a binary search
Target: 22
44
Another Example
The algorithm uses the following five steps to find the largest
integer.
45
Defining actions in FindLargest algorithm
46
Refinement
47
Generalization
48