Академический Документы
Профессиональный Документы
Культура Документы
Lecture 17
Algorithms II
1
Focus of the last lecture was on
Algorithms
Became familiar with the concept of algorithms:
1. Sequences
2. Conditionals
3. Loops
4
Start or stop
Process
Flowchart
Decision
Elements
Flow line
Connector
Off-page connector 5
This review was essential because we we
will be using these building blocks quite
often today.
6
Sequences
A sequence of instructions that are executed
in the precise order they are written in:
statement block 1
statement block 1
statement block 2 statement block 2
statement block 3
statement block 3
7
Conditionals
Select between alternate courses of action
depending upon the evaluation of a condition
If ( condition = true )
statement block 1 True False
Else condition
statement block 2
End if statement statement
block 1 block 2
8
Loops
Loop through a set of statements as long as a
condition is true
statement True
condition
block
False
9
We will now present the algorithm for a
problem whose solution is familiar to us
1. Pseudo code
2. Flowchart
3. Actual code
10
Problem Statement
Convert a decimal number into binary
11
Convert 75 to Binary
2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
2 1 0
0 1
1001011 12
We did write down the pseudo code for
this problem last time
13
Solution in Pseudo Code
1. Let the decimal number be an integer x, x > 0
2. Let the binary equivalent be an empty string y
3. Repeat while x > 0 {
Determine the quotient & remainder of x ÷ 2
y = CONCATENATE( remainder, y )
x = quotient
}
4. Print y
5. Stop 14
Q: Is this the only possible algorithm for
converting a decimal number into a binary
representation?
15
Tips on Writing Good Pseudo Code
• Use indention for improved clarity
• Do not put “code” in pseudo code – make your
pseudo code language independent
• Don’t write pseudo code for yourself – write it
in an unambiguous fashion so that anyone with
a reasonable knowledge can understand and
implement it
• Be consistent
• Prefer formulas over English language
16
descriptions
Start Flowchart of Decimal
to Binary Conversion
Get x
Print y
x is the decimal number
Stop y is the binary equivalent
17
1. Does the flowchart depict the “correct”
algorithm?
2. What do we mean by “correct”, or better
yet, what do we check for “correctness”?
3. One way is to check the algorithm for a
variety of inputs
4. Does it perform satisfactorily for:
• x=0?
• negative numbers?
• numbers with fractional parts? 18
Decimal to Binary Conversion in JavaScript
<SCRIPT>
x = 75; // x is the decimal number
y = “”; // y is the binary equivalent
while ( x > 0) {
remainder = x % 2;
quotient = Math.floor( x / 2 );
y = remainder + y; NOTE: Don’t
x = quotient; worry if you
} don’t
understand this
document.write(“y = ” + y);
code for now;
</SCRIPT> 19
you will - later!
Another Example: Sorting
Sort the following objects w.r.t. their heights
20
Expected Result
21
Strategy
There are many strategies for solving this
problem. We demonstrate a simple one:
Swap it with the one next to it if they are in the wrong order
Keep on repeating until you reach the last object in the list
22
Back to the Objects to be Sorted
23
Q: Is the list sorted?
A: No
24
Sorting: Step A1
25
Sorting: Step A1
Swap? Yes
26
Sorting: Step A2
27
Sorting: Step A2
Swap? Yes
28
Sorting: Step A3
29
Sorting: Step A3
Swap? No
30
Sorting: After Step A7
31
Q: Is the list sorted?
A: No
32
Sorting: Step B1
33
Sorting: Step B1
Swap? Yes
34
Sorting: Step B2
35
Sorting: Step B2
Swap? No
36
Sorting: After Step B7
37
Q: Is the list sorted?
A: No
38
Sorting: Step C1
39
Sorting: Step C1
Swap? No
40
Sorting: After Step C7
41
Q: Is the list sorted?
A: Yes
42
43
Let’s now look at this same process of
sorting being applied to a bigger list
44
Start Flowchart for the Sorting Process
list is an array containing the heights
Get list N is the total number of objects in the list
Yes No
n>N ?
n = n+1
Get list
Yes No
n>N ?
n = n+1
48
Pros and Cons of Flowcharts (1)
• I personally don’t find flowcharts very useful
50
Pros and Cons of Pseudo Code (1)
• Quite suitable for SW development as it is
closer in form to real code
52
With that we have reached the end of the
materials that we wanted to cover today.
53
Assignment # 6
There are many algorithms for sorting a list; Bubble
sort – the sorting algorithm discussed today in - is just
one example.