An algorithm is a step-by-step method for solving a problem or doing a task演算法是一步接者一步地解一個問題或完成某項工作的方法
An algorithm accepts a list of input data and creates a list of output data演算法會接受輸入資料串列並產生適當的輸出資料
Informal Definition (非正式定義)
A step-by-step method for
solving a problem or doing a task
(一步接者一步地解一個問題或
完成某項工作的方法) Input List (輸入串列) Output List (輸出串列) Algorithm (演算法)
Example (範例)
The algorithm uses the following five steps to find the largest integer由五個整數最大的整數的演算法
12, 8, 13, 9, 11
Input List (輸入串列) Output List (輸出串列) FindLargest Set Largest to 0
(將Largest設為0) Repeat the following step N times:
(重複下列步驟N次:) If the current number is greater than Largest,
set Largest to the current number.
(若目前的數字大於Largest內的值,
將目前的數字設定給Largest)
8.2
Three Constructs
(三種結構)
Three Constructs (三種結構)
Computer scientists have defined three constructs for a structured program or algorithm電腦科學家為結構化程式與演算法定義了三種結構
Sequence (循序)
Decision (selection) (決定、選擇)
Repetition (重複)
Sequence (循序)
do action 1
do action 2
…
…
…
…
…
…
…
…
…
…
do action n 執行動作 1
執行動作 2
…
…
…
…
…
…
…
…
…
…
執行動作 n
Decision (Selection) (決定、選擇)
if a condition is true,
then
else
do a series of actions do another series of actions if 條件為真,
then
else
執行一串動作 執行另一串動作
Repetition (重複)
while a condition is true,
do action 1
do action 2
…
…
…
…
…
…
…
do action n while 條件為真,
執行動作 1
執行動作 2
…
…
…
…
…
…
…
執行動作 n
Comments