0% found this document useful (0 votes)
66 views9 pages

#$&$6) $6 - & - ? (+ + (+) #$ 3 4-'#3 & - &$ 8#1@50 + - 6# - 0% &$2 8+#+A$ (& $6 :8$ $2 +# 4 $6 (Factorial)

This document discusses recursive functions and algorithms. It provides examples of recursive functions to calculate factorial, power, and binary search. It explains how recursion works by using a stack to remember previous function calls and return values. The time complexity of binary search is analyzed to be O(log n).
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views9 pages

#$&$6) $6 - & - ? (+ + (+) #$ 3 4-'#3 & - &$ 8#1@50 + - 6# - 0% &$2 8+#+A$ (& $6 :8$ $2 +# 4 $6 (Factorial)

This document discusses recursive functions and algorithms. It provides examples of recursive functions to calculate factorial, power, and binary search. It explains how recursion works by using a stack to remember previous function calls and return values. The time complexity of binary search is analyzed to be O(log n).
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

9*:5-70 14: ;(+)#$3

/&0( -(1&0&+ +2&3 4+ -5-&04$6( &+ -73% -8*"#$2% 4"+&+:


! "##$%& '() &#&*+$,)
-(-&#&*+$%./) / 0#&*+$%./) 1+%2/) 34#&+',256#
7&+&*589&'& 0#:*+$(): 7&+&9$#'%./, ;<#&(, 0+%=$8 Fibonacci
0>&8+52( '() 0#&*+$,)

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-1

!0-+(+)#$*<"% =2(+#&3=-*%

>#$&$6 )$6- &* -?(+* +(+)#$3 4-'#3=&- &$ 8#1@50+ -6#-=0% &$2
8+#+A$(&*<$6 <:8$*$2 +#*4$6 (factorial).
0! = 1 ,
1! = 1
2! = 1x2 = 2
3! = 1x2x3 = 6
4! = 1x2x3x4=24
5! = 1x2x3x4x5 = 120
!"#$%&'$: Na 4?$@$%,2$45 '(# factorial(int n) ( $@$8& &) 5@%2'+">5% '$
@&+&9$#'%./ .:@$%$4 =5'%.$< &.5+&8$4 n.
!"#$
int factorial(int n) {
int i, result=1;
for (i=1; i<=n; i++) {
result *= i;
}
return result;
}
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-2

;(+)#$3
B+=*<3 "(($*+ =&+ !+40+&*<: <+* =&0( >50#$,$#*<3.
/&0( 850#$,$#*<3 0 +(+)#$3 C#0=*$8$*-?&+* =+( '5A#%.,
@+$9+&&'%2$< <+* =+( "=$*$) 2A5*%&2$< &?9$+8=6#.
/&$( 8#$A#++&*=1 0 +(+)#$3 -,+(?D-&+* - &0( %&'#$ ()*+
,-.-/.0/123.+ 2-* 3.) (2,3* 3.,. E(+ +(+)#$*<1
28$8#1A#++ +8$&-5-?&+* +81:
"(+ 4'2 562%.-'+, 18$2 $#?D-&+* 0 -<&"5-=0 &$2 28$8#$A#:+&$% A*+
<:8$*-% *<#"% &*"% &'( 8+#+"&#'( &$2, <+*
"(+ 2)25/.6%* 4'2, <+&: &$ $8$?$ 0 -<&"5-=0 &$2 28$8#$A#:+&$%
$#?D-&+* '% =2()2+=1% <53=-'( &$2 28$8#$A#:+&$% =- :55-%
*<#1&-#-% &*"% &'( 8+#+"&#'(.

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-3

>+#:)-*A+ 2: >+#+A$(&*<1 ;#*4$6


;% $#?=$2- &.#+ &$( +(+)#$*<1 $#*=1 &0% factorial.
0! = 1 ,
1! = 1
4! = 1x2x3x4=24

2! = 1x2 = 2
3! = 1x2x3 = 6
5! = 1x2x3x4x5 = 120

;(+)#$*<1% F#*=1% Factorial

0! = 1
n! = n ! (n-1)! , n > 0

B3+ 9*+<$83%
;(+)#$*<1 B3+

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-4

>+#:)-*A+ 2: >+#+A$(&*<1 ;#*4$6


int factorial ( int n ) {
if (n == 0)
return 1;
else
return n ! factorial(n-1);
}

6
2

G<&"5-=0 factorial(3)
return 3 !

G<&"5-=0 factorial(2)

factorial(2);

return 2 !
1

G<&"5-=0 factorial(0)

G<&"5-=0 factorial(1)

factorial(1);

return 1 !
factorial(0);

return 1
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-5

H5$8$?0=0 +(+)#$3%
/- <:4- <53=0 $8$*+=)38$&- =2(:#&0=0% "(+ =6($5$ +81 5"7-*%
(stack frame) 7,&1##(326 #( 62 #3.842 (3$) #3.842 3.,
-/.0/123.+), +81 18$2 8$#-? (+ +(+=2#4-?.

I&+( *+ =2(:#&0=0 )*+<1J-* &0( -<&"5-=3 &0% - &0( <53=0 *+%


:550% =2(:#&0=0% $( )*+,&#+$( #"- ./',+#"."-, ( 0(&%1/'."
&)(.#+$23- <+* $% #$)(45- &#*67"#5- &0% %2&."#2+ #,)1/3$#$+
,25:==$(&+* "=+ =&0 =&$?@+ &$2 8#$A#:+&$%.

E&=* 1&+( 0 <504-?=+ =2(:#&0=0 &-#+&?=-* &$ 8-#*@:55$( &0(


<+5$6=+% =2(:#&0=0% 2)2#"/(326 2-* 3$ #3.842 A*+ (+ =2(-C*=&-?
<+($(*<: 0 -<&"5-=3 &0%.
;,$6 <:4- <53=0 *+% )*+)*<+=?+% -<&-5-?&+* =&$ )*<1 &0%
8-#*@:55$(, -?(+* -8*&#-8&3 <+* $ %&'#$ #,)2/3'#(9) 2-* 3.)
(2,3* &$2% (+(+)#$3).

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-6

>+#:)-*A+ 2: 96(+0 ;#*4$6


2) :")2$ (Power)
a0 = 1
an = an-1.a (n>=1)
int mpower(int a, int n) {
if (n==0)
return 1;
return a*mpower(a, n-1);
}
8*+,0&(9*
mpower(2,3) => 2*mpower(2,2)
= 2*2*mpower(2,1)
= 2*2*2*mpower(2,0)
= 2*2*2*1= 8

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-7

>+#:)-*A+ 3: 92+)*<3 ;(+D3&0=0 (;(:52=0)

;5*$"#& B%2/*$4: >?(+<+% K - n =&$*C-?+, 32;6).$<).+ +81 &$


*<#1&-#$ =&$ -A+56&-#$, <+* +<"#+*$% k.

3'/A$): L+ -7+<#*@.=$2- +( &$ k -?(+* =&$*C-?$ &$2 K.

12 15 17 18 30 33

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-8

92+)*<3 ;(+D3&0=0
:/*0(43 ;'*<3#".": @#?=<$2- &$ "=$ &$2 8?(+<+ <+* +8$,+=?D$2- +(
&$ k +(3<-* =&$ )-7*1 3 &$ +#*=&-#1 *=1. G8+(+5+@:($2- &0( ?)*+
)*+)*<+=?+ =&$ "*=1" 8$2 +% -()*+,"#-*.
!"#$%!"&'()$!"#$*+,-!"#$"-!"#$./0$
$$!"#$123$4$5-$6!76$4$"89:$
$$!"#$;!<:$
K=15?
$$36!1=$)$123$>4$6!76$/0$
$$$$;!<$4123?)6!768123/@A:$$
X
$$$!B$)*+;!<,$44$./$$
8 12 15 17 18 30 33
$$
$'=#C'"$;!<:$
0
1
2
3
4
5
6 n
$$$$=1D=$!B$)*+;!<,$>$./$
low
high
mid1
mid2
$
$$123$4$;!<$?$9:$
mid3
$$$$=1D=$!B$)*+;!<,$E$./$$
$
$6!76$4$;!<89:$
$$F$
M)*$ - (low+high)/2. N=&1=$ 0
$$'=#C'"$89:$
"<)$=0 +2&3 8$#-? (+ 28$,"#-* +81
F$$
28-#C-?5*=0 +<-#+?$2.
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-9

;(+)#$*<3 92+)*<3 ;(+D3&0=0


O+ 1#*+ )?)$(&+* =+( 8+#:-&#$

int BS_aux(int list[],int low,int high,int k)


{
K=15?
int mid;
X
if (low<=high) {
mid=low+(high-low)/2;
8 12 15 17 18 30
if (list[mid]==k)
0
1
2
3
4
5
return mid;
low
mid2 mid3 mid1
else if (k<list[mid])
return BS_aux(list,low,mid-1,k);
else
return BS_aux(list,mid+1,high,k);
}
return -1;
}

33
6

high

BinarySearch(int A[], int n, int k) {


int low = 0, high = n-1;
return BS_aux(A, low, high, k);
}
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-10

92+)*<3 ;(+D3&0=0 K#1($% G<&"5-=0%


P @+=*<3 8#:70 (=6A<#*=0) -<&-5-?&+* F(log2n) ,$#"% )05+)3:
!"#$%&'( 1

-> )*+ *,-$.&/* n/2 #-0 ,1.*"*,

!"#$%&'( 2
!"#$%&'( 3

!"#$%&'( 2

-> )*+ *,-$.&/ n/4 #-0 ,1.*"*,


-> )*+ *,-$.&/ n/8 #-0 ,1.*"*,

-> )*+ *,-$.&/ 1 '#-/3&1- #-0 ,1.*"*,

!"#$ %&"'(%)# * %+"% ,-.&/% "0 )"012%+0 %+"% 321


4(%.$3-0& #(. *"-%-051* n, n/2, n/4, n/8,, 4, 2, 1,
<==> 20,21,22,23,,2x " n
6- x &"789:&/ ,;'&+ 7-8$+ &"#&%-<& #- while loop

2 x ! n => log 2 2 x ! log 2 n => x ! log 2 n

Binary Search - F(log2n)


* K'#?% @5:@0 &0% A-(*<1&0&+%, 4-'#3=&- 1&* n -?(+* D2A1%
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-11

=.&,-&.%*3$32 >)25/.6%?) :6256%2#6?)


!"C#* &.#+ =2D0&3=+- &-C(*<"% A*+ &0( 2)1&,#$
(-2)2&$-36%?) 2&0./8@9) (- while, for, <&5.)
N=&1=$, 8$55$? 2&0*/6@.6 $#?D$(&+* 2)25/.6%1 (8.C. binary
search, etc)
Q"5$2- <:8$*+ -4$)$5$A?+ A*+ (+ +(+56$2- &0(
8$5285$<1&0&+ &"&$*'( +(+)#$*<.( -7*=.=-'(.
8.C. O(n) = 2#T(n/2) +1000n
O(n)=2n*T(n-1), n>0 <&5.
Q+ -5-&3=$2- ;(+)#$*<"% G7*=.=-*% &68$2 :628/(6 %26
A2#8&(,(
Q+ &*% -8*56=$2- - &0( B<@.5. 3$+ >)36%231#32#$+
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-12

;(:52=0 ;(+)#$*<3% 92+)*<3% ;(+D3&0=0


;% 7+(+)$6- &0( +(+)#$*<3 "<)$=0 &0% )2+)*<3% +(+D3&0=0%
int BS_aux(int list[],int low,int high,int k)
{
int mid;
if (low<=high) {
mid=low+(high-low)/2;
if (list[mid]==a)
return mid;
else if (k<list[mid])
return BS_aux(list,low,mid-1,k);
else
return BS_aux(list,mid+1,high,k);
}
return -1;
}

K=15?
X

8
0

low

12 15 17 18 30 33
1
mid2

mid1

high

mid3

;81 1&* @5"8$2- =- <:4- -<&"5-=0 &$ binary_search $*#:D-* *+


+<$5$24?+ n =&$*C-?'( =- n/2 =&$*C-?+ (-:( n -?(+* D2A1%).
G8$"('% &$ 8#1@50+ -A"4$2% n "A*(- &.#+ n/2.
/- <:4- @3+ C#-*+D1+=&- <+* 2 =2A<#?=-*% (&+ )2$ if statements)
O C#1($% -<&"5-=0% &0% binary_search -<,#:D-&+* - &0( +(+)#$*<3
=2(:#&0=0:
f(n) = f(n/2) + 2 // +(+)#$*<1 @3+
f(1) = 2
// =2(43<0 &-#+&*=$6
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-13

B<@.5.+ 3$+ >)36%231#32#$+


B<@.5.+ 3$+ >)36%231#32#$+
K#0=*$8$*$6- &$ @3+ &0% +(+)#$3% -8+(+508&*<:, "C#* (+ -<,#:=$2- &$
O(n) '% =2(:#&0=0 &0% @+=*<3% 8-#?8&'=0%, )2(:-*% &$2 n <+* =&+4-#"% &*"%.
=2*+$93
EC$2- &0( +(+)#$*<3 -7?='=0 &0% )2+)*<3% )*-#-6(0=0% (C"-., :628/(6 %26 A2#8&(,()
O(n) = T(n/2) + 2,
T(1) = 2

A*+ <:4- n$2

O1&-, +(&*<+4*=&.(&+% &$ O(n/2) - &0( &*3 &$2 8+?#($2O(n) = T(n/2) + 2


= T(n/4) + 2 + 2
= T(n/8) + 2 + 2 + 2
= (!8$#$6- &.#+ (+ +(&"J$2- 1&* )
= 2 + ... + 2 + 2 + 2

$!!#!!"
log2 n

,$#"%

G8$"('% 0 )2+)*<3 +(+D3&0=0 -<&-5-?&+* log2n @3+&+

Binary Search - F(log2n)

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-14

>+#:)-*A+ 1 - !"4$)$% &0% +(&*<+&:=&+=0%


EC$2- &0( +(+)#$*<3 -7?='=0
O(n) = 4#T(n/2) + n,
T(1) = 1

A*+ <:4- n$2

O1&-, +(&*<+4*=&.(&+% &$ O(n/2) - &0( &*3 &$2 8+?#($2O(n) = 4#T(n/2) + n


// G<&"5-=0 1
= 4(4#T(n/4) + n/2) + n
// G<&"5-=0 2
= 4R#O(n/4) + 2n + n
// >#:7-*%
= 4S#O(n/8) + 2R n + 2n + n
// G<&"5-=0 3
= ... !8$#$6- &.#+ (+ +(&"J$2- 1&*
= 4k#O(1) +2k-1n + +2R n + 2n + n
// k=log2n

=4

log2 n

+ n*

log2 n "1

#2

=2

log2 n 2

+ n * (2

i =0

log2 n

" 1)

// " 2i = 2 n +1 ! 1
i =0

= n 2 + n(n " 1 ) = 2n 2 " n ! O(n 2 )


!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-15

>+#:)-*A+ 2 - !"4$)$% &0% +(&*<+&:=&+=0%


D#%$#$
L+ 56=-&- &0( 8*$ <:&' +(+)#$*<3 -7?='=0 - &0( <@.5. 3$+
2)36%231#32#$+ (8#$=$C3 )-( -?(+* &68$2 )*+?#-* & @+=?5-2-)
O(0)=1
O(n)=2n*T(n-1), n>0
!"#$
O(n) = 2*n*T(n-1)
= 22*n*(n-1)*T(n-2)
= 23*n*(n-1)*(n-2)*T(n-3)
.(!8$#$6- &.#+ (+ +(&"J$2- 1&* )
= 2n*n*(n-1)*(n-2)*...*2*1*T(0)
= 2n*n!
- F(2nn!)
!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-16

Master Theorem
To Master Theorem +% -8*&#"8-* (+ @#?=<$2- 3 (+ -8+504-6$2- &0(
C#$(*<3 8$5285$<1&0&+ +(+)#$*<.( -7*=.=-'( 3"-., 5628/(6 %26
42#8&(,(.
:628/(6 %26 A2#8&(,(: -E. O(n) = T(n/2) + 2 2&&1 *E6 O(n)=2n*T(n-1)
;2&1 &$ 4-.#0+ 5() E/(61F(326 (+ &$( 2-.)$.)("#(3( +55:
8$#-?&- (+ &$( C#0=*$8$*3=-&- A*+ &0( (-2&'@(,#$ 2#%'#(9).

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-17

Master Theorem - G72/.0'


>)25/.6%' :,256%' >)2F'3$#$
O(n) = T(n/2) + 2 A*+ <:4- n$2
T(1) = 2
a=1, b=2, c=2, d=0 ()-% &68$ Master)
% a=1 <+* bd=20= 1
% T(n) is O(ndlogn)
% T(n) is O(n0logn)
% T(n) is O(logn)

!"# 035 $%&' $()%&*+* ,-. /0123.4%. 1.- 50. 678. ,-. 678. 9:%0.
!"#$% &'( )*+,+(-*.( Demetrios Zeinalipour University of Cyprus

14-18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy