algorithms and flowcharts ii
DESCRIPTION
algoritma and flowchartTRANSCRIPT
![Page 1: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/1.jpg)
ALGORITHMS AND
FLOWCHARTS
Part #2
![Page 2: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/2.jpg)
Three Constructs
![Page 3: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/3.jpg)
Flowcharts for Three Constructs
![Page 4: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/4.jpg)
Pseudocode for Three Constructs
![Page 5: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/5.jpg)
Examples-1 calculate 12 + 22 + ... + n2
Pseudocode
Input: n
sum 0
i 1
Repeat the following
three steps while i n:
sq i * i
sum sum + sq
i i + 1
Output: sum
A B
Set A to the
value of B
![Page 6: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/6.jpg)
Examples-1:
Flowchart
n
sum 0
i 1
i n ?
sq i * i
sum sum + sq
i i + 1
sum
No
Yes
Input / output
Processing step
Decision
![Page 7: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/7.jpg)
Examples-2:
1. Start at pos0, facing dir0
2. If wall in front,
turn 90º clockwise
else
go forward
3. If not back to initial
position / direction
proceed to Step 2
else
stop
dir dir + 90º
pos = pos0
and dir = dir0?
Yes No
Yes
No
pos pos0
dir dir0
Step forward
Input:
pos0, dir0
Stop
Wall in front?
![Page 8: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/8.jpg)
Examples-3:
Sort the following objects based on their
heights
![Page 9: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/9.jpg)
Output
Expected result
![Page 10: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/10.jpg)
Strategy
There are many strategies for solving this
problem. We demonstrate a simple one:
Repeat the following steps while the list is un-sorted:
Start with the first object in the list
Swap it with the one next to it if they are in the wrong order
Repeat the same with the next to the first object
Keep on repeating until you reach the last object in the list
![Page 11: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/11.jpg)
Back to The Objects to be Sorted
![Page 12: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/12.jpg)
12
Q: Is the list sorted?
A: No
![Page 13: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/13.jpg)
13
Sorting: Step A1
![Page 14: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/14.jpg)
14
Sorting: Step A1
Swap? Yes
![Page 15: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/15.jpg)
15
Sorting: Step A2
![Page 16: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/16.jpg)
16
Sorting: Step A2
Swap? Yes
![Page 17: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/17.jpg)
17
Sorting: Step A3
![Page 18: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/18.jpg)
18
Sorting: Step A3
Swap? No
![Page 19: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/19.jpg)
19
Sorting: After Step A7
![Page 20: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/20.jpg)
20
Q: Is the list sorted?
A: No
![Page 21: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/21.jpg)
21
Sorting: Step B1
![Page 22: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/22.jpg)
22
Sorting: Step B1
Swap? Yes
![Page 23: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/23.jpg)
23
Sorting: Step B2
![Page 24: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/24.jpg)
24
Sorting: Step B2
Swap? No
![Page 25: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/25.jpg)
25
Sorting: After Step B7
![Page 26: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/26.jpg)
26
Q: Is the list sorted?
A: No
![Page 27: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/27.jpg)
27
Sorting: Step C1
![Page 28: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/28.jpg)
28
Sorting: Step C1
Swap? No
![Page 29: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/29.jpg)
29
Sorting: After Step C7
![Page 30: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/30.jpg)
30
Q: Is the list sorted?
A: Yes
![Page 31: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/31.jpg)
31
![Page 32: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/32.jpg)
32
Start
n = n+1
Get list
list
sorted?
Stop
SWAP
list[n], list[n+1]
list is an array containing the heights
N is the total number of objects in the list
Flowchart for the Sorting Process
No
Yes
n = 0 list[n] >
list[n+1]?
Yes No n>N ?
Yes
No
![Page 33: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/33.jpg)
Variables
Algorithms usually work with variables
A variable is a “named container”
A variable is like a slate on which a
value can be written and later erased
and replaced with another value
sum sum + sq
sum
![Page 34: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/34.jpg)
Properties of Algorithm
Compactness: an algorithm can use
iterations or recursion to repeat the
same steps multiple times
Generality: the same algorithm applies
to any “size” of task or any input values
Abstractness: an algorithm does not
depend on a particular computer
language or platform (although it may
depend on the general computing
model)
![Page 35: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/35.jpg)
Properties of Algorithm
Input: n
sum 0
i 1
Repeat the following
three steps while i n:
sq i * i
sum sum + sq
i i + 1
Output: sum
Compact: the same length regardless
of n, thanks to iterations the
algorithm repeats the same
instructions many times, but with
different values of the variables
(The “running time” depends on n, of course)
General: works for any n
![Page 36: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/36.jpg)
Properties of Algorithm
function addSquares(n : integer)
: integer;
var
i, sum : integer;
begin
sum := 0;
for i := 1 to n do begin
sum := sum + i * i
end;
addSquares := sum;
end;
Abstract:
Pascal
C/C++
Java
public class MyMath
{
public static int
addSquares(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i * i;
return sum;
}
}
int addSquares(int n)
{
int i, sum = 0;
for (i = 1; i <= n; i++)
sum += i * i;
return sum;
}
![Page 37: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/37.jpg)
Iterations
Repeat the same sequence of
instructions multiple times
Start with initial values of variables
Values of some of the variables
change in each cycle
Stop when the tested condition
becomes false
Supported by high-level
programming languages
![Page 38: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/38.jpg)
Iterations: while Loop in Java
For example:
while (<this condition holds>)
{
... // do something
}
while (i <= n)
{
sum += i * i; // add i * i to sum
i++; // increment i by 1
}
![Page 39: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/39.jpg)
Iterations: while Loop in Java
for (<initial setup>; <as long as this condition holds>;
<adjust variable(s) at the end of each iteration>)
{
... // do something
}
for ( int i = 1; i <= n; i++)
{
sum += i * i; // add i * i to sum
}
For example: Increment i
by 1
![Page 40: Algorithms and Flowcharts II](https://reader030.vdocument.in/reader030/viewer/2022012901/5695d0211a28ab9b02911b60/html5/thumbnails/40.jpg)
Assignments
Write an algorithm in pseudocode that finds
the average of two numbers
Write an algorithm to change a numeric grade
to a letter grade.
Write an algorithm to find the largest of a set
of numbers. You do not know the number of
numbers.
Write an algorithm to find the largest of 1000
numbers.