Writing Structured Programs
Ernastuti
Four iteration statements
1. loop-repeat
2. while-repeat
3. loop-until-repeat
4. for-repeat
Loop-repeat
loop read(x) if x = eof then exit endif call PROCESS(x) repeat
S
While-repeat
while cond do S repeat
Scondtrue
false
loop-until-repeat
loop S until cond repeat
S cond
true
false
For-repeat
for vble start to finish by increment do S repeat
S cond
true
false
for-repeat
vble start fin finish incr increment while ( vble – fin ) * incr 0 do S vble vble + incr repeat
Scondtrue
false
for vble start to finish by increment do S repeat
The case for CASEif cond 1 then S1
else if cond 2 then S2
else if cond 3 then S3
. .
else if cond n then Sn
else Sn+1
endif endif …endif
case : cond 1 : S1 : cond 2 : S2
: cond n : Sn
: else : Sn+1
endcase
Find the maximum of n numbers, n > 0
procedure MAX(A,n,j)// Set j so that A(j) is the maximum in A(1:n), n>0 // xmax A(1); j 1 for i 2 to n do If A(i) >xmax then xmax A(i); j i ; endif repeatend MAX
Find the maximum of n numbers, n > 0
procedure MAX1(i) //this is a function which returns the largest integer k such that
A(k) is the maximum element in A(i:n)// global integer n, A(1:n), j,k ; integer i if i < n then jMAX1(i+1) if A(i) >A(j) then k i else k j endif else k n endif return(k)end MAX1
procedure MAX2(i) local integer j,k; global integer n, A(1:n); integer i integer STACK(1:2*n); top 0L1: if i < n then top top + 1; STACK(top) i top top + 1; STACK(top) 2; i i + 1 go to L1 L2: j STACK(top); top top – 1 if A(i) >A(j) then k i else k j endif else k n endifIf top=0 then return(k) else addrSTACK(top); top top – 1 iSTACK(top); top top – 1 top top + 1; STACK(top) k If addr = 2 then go to L2 endif endifend MAX2
procedure MAX3(A,n) integer i,k,n; ik-n while i>1 do ii-1 if A(i) >A(k) then ki endif repeat return(k) end MAX3
Read a set of values until their sum exceeds a predefined limit, say n
y0
while y n do
read(x)
yy+x
repeat
Read in n values and process each one in some way
i1
while i n do
read(x)
call PROCESS(x)
i i + 1
repeat
for i 1 to n do read(x) call PROCESS(x)repeat
Recursion
The fibonacci sequence 1,1,2,3,5,8,13,21,34,… is defined as
F0 = F1 = 1, Fi = Fi-1 + Fi-2 , i > 1
procedure F(n) //returns the nth Fibonacci number// Integer n If n 1 then return(1) else return(F(n-1) + F(n-2)) endifend F
procedure F1(n) // a function which returns the nth Fibonacci number// If n < 2 then return(1) else return(F2(2,n,1,1)) endifend F1
procedure F2(i,n,x,y) If i n then call F2(i+1,n,y,x+y) endif return(y)end F2
Computing the greatest common divisor of two nonnegative integers
For example : gcd(22,8)= ?
22 = 1 x 2 x 11 8 = 1 x 23 gcd(22,8) = 2
Another way : gcd(22,8)=gcd(8,6)=gcd(6,2)=gcd(2,0)=2
Computing the greatest common divisor of two nonnegative integers
For example : gcd(21,13)= ?
21 = 1 x 3 x 713 = 1 x 13 gcd(21,13) = 1
Another way : gcd(21,13)=gcd(13,8)=gcd(8,5)=gcd(5,3)=
gcd(3,2)=gcd(2,1)=gcd(1,0)=1
Computing the greatest common divisor of two nonnegative integers
procedure GCD(a,b)
// assume a > b 0 //
if b = 0 then return(a)
else return(GCD(b,a mod b))
endif
end GCD
Computing the greatest common divisor of two nonnegative integers
procedure GCD1(a,b)
L1: if b=0 then return(a)
else tb; ba mod b; a t; go to L1
endif
end GCD1
Computing the greatest common divisor of two nonnegative integers
procedure GCD2(a,b)
while b 0 do
tb; ba mod b; a t
repeat
return(a)
end GCD2
A procedure which searches for x in A(1:n)
procedure SEARCH(i) //if there exists an index k such that A(k) = x in A(i:n), then the
first such k is returned else zero is returned//
global n,x,A(1:n) case : i> n : return(0) : A(i) = x : return(i) : else : return(SEARCH(i+1)) endcaseend SEARCH
Analyzing Algoriths
x x + y 1
for i 1 to n do x x + y nrepeat
for i 1 to n do for i 1 to n do x x + y n2 repeatrepeat
Orders of magnitude
• The order of magnitude of a statement refers to its frequency of execution
while
• The order of magnitude of an algorithm refers to the sum of the frequencies of all of its statements
• Determining the order of magnitude of an algorithm is very important and producing an algorithm which is faster by an order of magnitude is a significant accomplishment.
• The a priori analysis of algorithms is concerned chiefly with order of magnitude determination
• Fortunately there is a convinient mathematical notation for dealing with this concept.
• A priori = berdasarkan teori drpd kenyataan yg sebenarnya
Insertion Sort
Insertion-Sort(A) cost times1 for j 2 to length[A] c1 n2 do key A[j] c2 n-13 Insert A[j] into the sorted sequence A[1..j-1] 0 n-14 i j -1 c4 n-15 while i >0 and A[i]>key c5 tj
6 do A[i+1]A[i] c6 (tj – 1)7 i i -1 c7 (tj – 1)8 A[i+1] key c8 n-1
• The running time of the algorithm is the sum of running time for each statement executed;
• To compute (n), the running time of Insertion-Sort, we sum the products of the cost and times columns, obtaining
(n) = c1n + c2(n-1) + c4(n-1)+ c5 tj +
c6 (tj – 1) + c7 (tj – 1) + c8(n-1)