Professional Documents
Culture Documents
A B A B
ACTION
t0 t1
Pass k = 1
Pass k = 2
And so on
The initial & final states:
T0 : random sequence of values
Algorithm Data_Order
T1 : ordered sequence of values
Where:
Algorithm Data_Order
Given N random data. Order them so that data#1 < data #2 <
… < data #N
Description:
For pass k = 1,2, …, N-1
1. Find the smallest value (min) from k = 1 to k = N
2. Exchange min with data#k
START
k = k+1
1. Find min
2. Position min @ k
K=4 ?
YES
STOP
1. Find min
• Not detail enough
• Must be refined
Refining Step-1
1.1 Assume data #k is the min (temporary)
1.2 compare min with data #j; where j =(k+1), (k+2), .. , N
if data #j < min, then data #j becomes the new min
Refining Step-1.2
1.2.1 For j = (k+1), (k+2), …, N do:
if data #j < min
then data #j becomes the new min
2. Position min @ k
Refining:
2.1 put data #k to a temporary place (temp)
2.2 put the min on the place of the data #k (formerly)
2.3 put the temp on the min (formerly)
The final algorithm
Algorithm Data_Order
Given N numbers of random data. Make an order of data so that:
data#1 < data #2 < … < data #N
Description:
For pass k = 1,2, …, N-1, do the following:
{1. Find the smallest value (min) from k = 1 to k = N}
1.1 assume that data #k is the min
1.2 For j = (k+1), (k+2), …, N do:
if data #j < min
then data #j becomes the new min
{2. Exchange min with data#k }
2.1 put data #k to a temporary place (temp)
2.2 put the min on the place of the data #k (formerly)
2.3 put the temp on the min (formerly)
I leave the flowchart for
the next week’s homework
Basic Structures of Algorithms
Sequence
Selection
Repetition
Basic Structures of Algorithms Sequence
A1 A1
THE RESULT
A2 A3 ?
A3 A2
A B
A4 A4
C
Basic Structures of Algorithms Selection
Conditions
Recall:
if data #j < min then
data #j = new min
The general structure
If condition then
action
Basic Structures of Algorithms Selection
Example
Your example …
Basic Structures of Algorithms Selection
Another structure
If condition then
action 1
else
action 2
indenting
Your example …
Basic Structures of Algorithms Repetition
Description
4. Write “I promise that I will never forget to do my homework”
5. Write “I promise that I will never forget to do my homework”
6. Write “I promise that I will never forget to do my homework”
7. Write “I promise that I will never forget to do my homework”
8. Write “I promise that I will never forget to do my homework”
…
500 Write “I promise that I will never forget to do my homework”
???
Basic Structures of Algorithms Repetition
Algorithm Writing_500_sentences
Description:
sentenceNumber = 0
repeat
write “I promise that I will never forget to do my homework”
sentenceNumber = sentenceNumber +1
General Structure
Repeat
action
Until condition
A combination