Vous êtes sur la page 1sur 10

# METODA DIVIDE ET IMPERA

## NOTIUNI INTRODUCTIVE APLICATII DIVIDE ET IMPERA:

Turnurilor din Hanoi; Sortar ra!ida ; Sortar !rin int r"la#ar ; Sortar !rin in# rti \$inara;
CONCLU%II

&

NOTIUNI INTRODUCTIVE

M toda d !ro'ra(ar DIVIDE ET IMPERA "on#ta in i(!artir a !ro\$l ( i initial d di( n#iuni )n* in doua #au (ai (ult !ro\$l ( d di( n#iuni r du# +In ' n ral # , "uta i(!artir a in doua #u\$!ro\$l ( d di( n#iuni a!ro,i(ati- 'al #i anu( )n.&* + I(!artir a in #u\$!ro\$l ( ar lo" !ana "and di( n#iun a a" #tora d -in #u/i"i nt d (i"a ! ntru a /i r 0ol-at in (od dir "t1"a0ul d \$a0a2+Du!a r 0ol-ar a " lor doua #u\$!ro\$l ( # , "uta /a0a d "o(\$inar a r 0ultat lor in - d r a r 0ol-arii intr 'ii !ro\$l ( + M toda DIVIDE ET IMPERA # !oat a!li"a in r 0ol-ar a un i !ro\$l ( "ar ind !lin #t ur(atoar l "onditii : # !oat d #"o(!un in 1 doua #au (ai (ult 2 #u!ro\$l ( ; a" #t #u!ro\$l ( #unt ind ! nd nt una /ata d alta 1o #u\$!ro\$l (a nu # r 0ol-a ! \$a0a alt ia #i nu # /olo# #t r 0ultat " l ilalt 2; a" #t #u\$!ro\$l ( #unt #i(ilar "u !ro\$l (a initiala; la randul lor #u\$!ro\$l ( l # !ot d #"o(!un 1da"a #t n " #ar2 in alt #u\$!ro\$l ( (ai #i(!l ; a" #t #u\$!ro\$l ( #i(!l # !ot #olutiona i( diat !rin al'orit(ul #i(!li/i"at+ D oar " !utin !ro\$l ( ind !lin #" "onditiil d (ai #u# 3a!li"ar a ( tod i #t d #tul d rara+ Du!a "u( #u' r a0a #i nu( l 4d #!art #i #ta!an #t 4 ta! l r 0ol-arii un i !ro\$l ( 1 nu(ita !ro\$l (a initiala 2 in DIVIDE ET IMPERA #unt : d #"o(!un r a !ro\$l ( i initial in #u\$!ro\$l ( ind ! nd nt 3#(ilar !ro\$l ( i d \$a0a 3d di( n#iuni (ai (i"i ; d #"o(!un r a tr !tata a #u\$!ro\$l ( lor in alt #u\$!ro\$l ( din " in " (ai #i(!l 3!ana "and # !ot r 0ol-a i( diata 3!rin al'orit(ul #i(!li/i"at ; r 0ol-ar a #u\$!ro\$l ( lor #i(!l ; "o(\$inar a #olutiilor 'a#it ! ntru "on#truir a #olutiilor #u\$!ro\$l ( lor d di( n#iuni din " in " (ai (ari ; "o(\$inar a ulti( lor #olutii d t r(ina o\$tin r a #oluti i !ro\$l ( i initial + M toda DIVIDE ET IMPERA ad(it o i(!l ( ntar r "ur#i-a 3d or " #u\$!ro\$l ( l #unt #i(ilar !ro\$l ( i initial 3 dar d di( n#iuni (ai (i"i + Prin"i!iul /unda( ntal al r "ur#i-itatii #t autoa! lar a unui #u\$!ro'ra( "and a" #ta #t a"ti-;" a " # inta(!la la un ni- l 3# inta(!la la ori" ni- l 3a-and 'ri5a #a a#i'ura( "onditia d t r(inar al a! lurilor r ! tat +A# (anator # inta(!la #i in "a0ul ( tod i DIVITE ET IMPERA ; la un anu(it ni- l #unt doua !o#i\$ilitati : #6a a5un# la o 1#u\$2 !ro\$l (a #i(!la " ad(it o r 0ol-ar i( diata "a0 in "ar # r 0ol-a 1#u\$2!ro\$l (a #i # r -in din a! l 1la #u\$!ro\$l (a ant rioara3d di( n#iuni (ai (ari2; #6a a5un# la o 1#u\$2 !ro\$l (a "ar nu ad(it o r 0ol-ar i( diata 3 "a0 in "ar o d #"o(!un ( in doua #au (ai (ult #u\$!ro\$l ( #i ! ntru /i "ar din l # "ontinua a! luril r "ur#i- 1al !ro" durii #au /un"ti i2+ In ta!a /inala a ( tod i DIVIDE ET IMPERA # !rodu" "o(\$inar a #u\$!ro\$l ( lor 1r 0ol-at d 5a2 !rin # "- nt l d r - nir din a! luril r "ur#i- + Eta! l ( tod i DIVIDE ET IMPERA 1!r 0 ntat ant rior2# !ot r !r 0 nta !rin ur(atorul #u\$!ro'ra( ' n ral 1!ro" dura #au /un"ti 2r "ur#i- ,!ri(at in li(\$a5 natural: Su\$!ro'ra( DIVIMP 1PRO72; Da"a PRO7LEMA PRO7 #t #i(!la Atun"i # r 0ol-a #i # o\$tin #olutia SOL 8

Alt/ l ! ntru i913: , "uta DIVIMP1PRO72 #i # o\$tin SOL1; S "o(\$ina #olutiil SOL 13+++ 3SOL ; #i # o\$tin SOL; S/ar#it <#u\$!ro'ra(; D "i 3#u\$!ro'ra(ul DIVIMP # a! l a0a ! ntru !ro\$l (a initiala PRO7;a" a#ta ad(it d #"o(!un r a in ; #u\$!ro\$l ( #i(!l ;! ntru a" #t a # r a! l a0a r "ur#i- #u\$!ro'ra(ul ;in /inal # "o(\$ina #olutiil a" #tor ; #u\$!ro\$l ( + D o\$i" i !ro\$l (a initiala # d #"o(!un in doua #u\$!ro\$l ( (ai #i(!l ; in a" #t "a0 ta! l ' n ral al ( tod i DIVIDE ET IMPERA # !ot r !r 0 nta "on"r t3in li(\$a5 !# udo"od 3!rintr6o !ro" dura r "ur#i-a a#t/ l : Pro" dura DIVIMP1li3l#3#ol2; Da"a 11l#6li2=9 !#2 Atun"i RE%OLVA 1li3l#3#ol2; Alt/ l DIVIDE 1li3(3l#2; DIVIMP1li3(#ol12; DIVIMP1(3l#3#ol&2; COM7INA1#ol13#ol&3#ol2; S/ar#it< !ro" dura; Pro" dura DIVIMP # a! l a0a ! ntru !ro\$l (a initiala "ar ar di( n#iun a intr li(ita in/ rioara 1li2 #i li(ita in/ rioara1l#2;da"a 1#u\$2!ro\$l (a #t #i(!la 1l#6li=9 !#23atun"i !ro" dura RE%OLVA ii a/la #olutia i( diat #i # !rodu" intoar" r a din a! lul r "ur#i-;da"a 1#u\$2!ro\$l (a #t 1in"a2 "o(!l ,a 3atun"i !ro" dura DIVIDE o i(!art in doua #u\$!ro\$l ( 3al 'and !o0itia ( intr li(it l li #i l# ;! ntru /i "ar din " l doua #u\$!ro\$l ( # r a! l a0a r "ur#i- !ro" dura DIVIMP; in /inal 3la intoar" ril din a! luri # !rodu" "o(\$inar a " lor doua #oluitii #ol1 #i #ol& !rin a! lul !ro" durii COM7INA+

## PROBLEMA TURNURILOR DIN HANOI

Pr 0 ntar a al'orit(ului r 0ol-arii >i tr i ti5 - rti"al notat A373C +P ti5a A # 'a# #" a# 0at n di#"uri d dia( tr di/ rit 3in ordin a "r #"atoar a dia( tr lor3!ri-ind d #u# in 5o# + Initial 3ti5 l 7 #i C #unt 'oal +Sa # a/i# 0 toat (utaril !rin "ar di#"uril d ! ti5a A # (uta ! ti5a 7 3 in a" a#i ordin 3/olo#ind "a ti5a d (an -ra C #i r ##! "tand ur(atoar l r 'uli: la /i "ar !a# # (uta un #in'ur di#"; un di#" # !oat a# 0a nu(ai ! #t un di#" "u dia( trul (ai (ar + R 0ol-ar a a" #t i !ro\$l ( # \$a0 a0a ! ur(atoar l "on#id r nt lo'i" : 6da"a n91 3atun"i (utar a #t i(( diata A71(ut di#"ul d ! A ! 72; da"a n9&3atun"i #irul (utarilor #t : AC3A73C7; da"a n?& !ro" da( a#t/ l : 6 (ut 1n612di#"uri AC; 6 (ut un di#" A7 ; 6 (ut " l 1n612di#"uri C7+ O\$# r-a( "a !ro\$l (a initiala # d #"o(!un in tr i #u\$!ro\$l ( (ai #i(!l 3 #i(ilar !ro\$l ( i initial : (ut 1n612di#"uri AC 3(ut ulti(ul di#" ! 7 3(ut " l 1n612di#"uri C66 ?7+Di( n#iunil a" #tor #u\$!ro\$l ( #unt : n61313n61+

A" #t #u\$!ro\$l ( #unt ind ! nd nt 3d oar " ti5 l initial 1 ! "ar #unt di#!u# di#"uril 2 3ti5 l /inal #i ti5 l int r( diar #unt di/ rit +Nota( H1n3A373C29#irul (utarilor a n di#"uri d ! A ! 73 /olo#ind C+ PENTRU n91 A7 n?1 H1n3A373C29 H1n613A3C3723A73 H1n613C373A2 !ro'ra( turnuril <Aanoi; -ar n:\$Bt ; !ro" dur Aanoi1n:\$Bt ;a3\$3":"Aar2; \$ 'in i/ n91 tA n Crit ln1a3DD3\$2 l# \$ 'in Aanoi1n613a3"3\$2; Crit ln1a3DD3\$2; Aanoi1n613"3\$3a2; nd; nd; \$ 'in Crit 1Enr di#"uri ! ti5a A 9D2;r adln1n2; Crit ln1E(utaril #unt ur(atoar l :D2; Aanoi1n3DAD3D7D3DCD2; r adln;r adln; nd+

## Sortare rapida(quick ort!

Un ta\$lou V # "o(!l t a0a "u n l ( nt nu( r r al +Sa # ordon 0 "r #"ator /olo#ind ( toda d #ortar ra!ida + Solutia !ro\$l ( i # \$a0 a0a ! ur(atoar l ta! i(!l ( ntat in !ro'ra(ul !rin"i!al: # a! l a0a !ro" dura Fui": "u li(ita in/ rioara li91 #i li(ita #u! rioara l#9n; /un"tia!o0 r ali0 a0a (utar a l ( ntului -)i* ,a"t ! !o0itia " o -a o"u!a a" #ta in - "torul /inal ordonat ; /un"tia!o0 intoar" 1in : 2 !o0itia o"u!ata d a" #t l ( nt; in a" #t / l 3- "torul V # i(!art in doua !arti : li G:61 #i :H1Gl#; ! ntru /i "ar din a" #t !arti # r a! l a0a !ro" duraFui":3"u li(it l (odi/i"at "or #!un0ator ; in a" #t / l 3!ri(ul l ( nt din /i "ar !art -a /i !o0itionat ,a"t ! !o0itia /inala " o -a o"u!a in - "torul /inal ordonat 1/un"tia!o02; /i "ar din " l doua !arti -a /i 3a#t/ l 3in!artita in alt doua !arti ;!ro" #ul "ontinua !ana "and li(it l !artilor a5un' #a # #u!ra!una 3" a " indi"a "a toat l ( nt l - "torului au /o#t (utat ,a"t ! !o0itiil " l -or o"u!a in - "torul /inal ;d "i - "torul #t ordonat ; in a" #t (o( nt # !rodu" intoar" ril din a! luril r "ur#i- #i !ro'ra(ul i#i t r(ina , "utia + !ro'ra( Fui":#ort; tB! - "tor9 arraB )1++IJ* o/ r al ; -ar -:- "tor; i3n3::int ' r; /un"tion !o01li3l#:int ' r2:int ' r; I

-ar i353(odi3(od53(:int ' r; (an:r al; \$ 'in i:9li; 5:9l#; (odi:9J; (od5:961; CAil i=5 do \$ 'in i/ -)i*?-)5* tA n \$ 'in (an:9-)i*; -)i*:9-)5*; -)5*:9(an; (:9(odi ; (odi:96(od5; (od5:96(; nd; i:9iH(odi; 5:95H(od5; nd; !o0:9i; nd; !ro" dur Fui":1li3l#:int ' r2; \$ 'in i/ li=l# tA n \$ 'in :::9!o01li3l#2; Fui":1li3:612; Fui":1:H13l#2; nd; nd; \$ 'in Crit 1E"at l ( nt ar - "torul K9D2;r adln1n2; /or i:91 to n do \$ 'in Crit 1Eta#tati l ( ntul E3i3D9D2; r adln1-)i*2; nd; Fui":113n2; Crit ln1E- "torul ordonat #t :D2; /or i:91 to n do Crit ln1-)i*2; r adln; nd+

OBSERVATIE
a"a l ( ntul # a/la in #tan'a 3atun"i # "o(!ara "u l ( nt l din dr a!ta lui #i # #ar 15:956 d 12 l ( nt l (ai (ari d "at l ; da"a l ( ntul # a/la in dr a!ta 3atun"i # "o(!ara "u l ( nt din #tan'a lui #i # #ar 1i:9iH12 l ( nt l (ai (i"i d "at l+

## Sortare pri" i"terc#a are(\$er%e ort!

Ta\$loul unidi( n#ional V # "o(!l t a0a "u n nu( r r al + Sa # ordon 0 "r #"ator /olo#ind #ortar !rin int r"la#ar + Sortar a !rin int r"la#ar # \$a0 a0a ! ur(atoar a lo'i"a : - "torul V # i(!art 3!rin in5u(atatiri #u"" #i- 3in - "tori din " in " (ai (i"i ;"and # atin' - "torii d (a,i( doua l ( nt 3/i "ar dintr a" #tia # ordon a0a !rintr6o #i(!la "o(!arar a l ( nt lor ;"at doi a#t/ l d (ini6 - "tori ordonati # int r"la# a0a #u"" #i- !ana # a5un' iar la - "torul V+ !ro'ra( ( r' #ort; tB! - "tor9arraB)1++IJ* o/ r al ; -ar -:- "tor ;n3i:Cord; !ro" dur #"Ai(\$a1li3l#:Cord;-ar a:- "tor2; -ar (an:r al; \$ 'in i/ a)li*?a)l#* tA n \$ 'in (an:9a)li*; a)li*:9a)l#*; a)l#*:9(an; nd; nd; !ro" dur int r"la#1li3(3l#:Cord;-ar a:- "tor2; -ar \$:- "tor:i3:3!35:Cord; \$ 'in i:9li; 5:9(H1; ::9J; CAil 1i=9(2and15=9l#2 do \$ 'in in"1:2; i/ a)i* =a)5* tA n \$ 'in \$):*:9a)i*; in"1i2; nd l# \$ 'in \$):*:9a)5*; in"152; nd; nd; i/ i=9( tA n /or !:9i to ( do \$ 'in1 in"1:2;\$):*:9a)!*; nd; i/ 5=9l# tA n /or !:95 to l# do \$ 'in in"1:2 ;\$):*:9a)!*; nd; ::9J; /or !:9li to l# do \$ 'in in"1:2; a)!*:9\$):*; nd; nd; M

!ro" dur di-i1li3l#:Cord; -ar a:- "tor2; -ar (:Cord; \$ 'in i/ 1l#6li2=91 tA n #"Ai(\$a1li3l#3a2; l# \$ 'in (:91liHl#2di- &; di-i1li3(3a2; di-i1(H13l#3a2; int r"la#1li3(3l#3a2; nd; nd; \$ 'in Crit 1E"at l ( nt ar - "torulK9D2;r adln1n2; /or i:91 to n do \$ 'in Crit 1Eta#tati l ( ntulD3i3D9D2; r adln1-)i*2; nd; di-i113n3-2; Crit ln1E- "torul #ortat #t :D2; /or i:91 to n do Crit ln1-)i*2; nd+

OBSERVATII
( "ani#(ul ' n ral d ti! Di-id t I(! ra # 'a# #t i(!l ( ntat in !ro" dura di-i ; a#t/ l d a\$ordar a !ro\$l ( i #ortarii unii - "tor "ondu" la "ono(i d ti(! d "al"ul o 3d oar " o! ratia d int r"la#ar a doi - "tori d 5a ordonati #t /oart ra!ida 3iar ordonar a ind ! nd nta " lor doua 5u(atati1(ini6 - "tori2 "on#u(a in total a!ro,i(ati- a doua !art din ti(!ul "ar ar /i n " #ar ordonarii - "torului luat "a intr ' +

## Sortare pri" i" ertie &i"ara

Sa # ordon 0 "r #"ator un ta\$lou unidi( n#ional V d n nu( r r al 3/olo#ind #ortar a !rin in# rti \$inara + P ntru /i "ar l ( nt -)i* # !ro" d a0a in !atru !a#i: # "on#id ra ordonat l ( nt l -)1*3-)&*3G+3-)i61*; # "auta !o0itia : ! "ar ur( a0a #6o o"u! -)i* intr l ( nt l -)1*3-)&*3G3-)i61* 1!ro" dura !o0 !rin "autar \$inara2; # d !la# a0a #!r dr a!ta l ( nt l din !o0itiil :3:H13G3n1!ro" dura d !la#ar 2; in# r a0a l ( ntul -)i* in !o0itia : 1!ro" durad !la#ar 2; # o\$tin o #u"" #iun d :H1 l ( nt ordonat "r #"ator+ !ro'ra( #ortar <\$inara; tB! - "tor 9arraB)1++IJ* o/ r al ; -ar n3:3i:int ' r; -:- "tor; /un"tion !o01li3l#3i:int ' r2:int ' r; -ar (:int ' r; \$ 'in i/ li9l# tA n N

i/ -)i*=-)5* tA n !o0:9li l# !o0:9i l# i/ l#6li91 tA n i/ -)i*=-)l#* tA n i/ -)i*?9-)li* tA n !o0:9l# l# !o0:9li l# !o0:9i l# \$ 'in (:91liHl#2di- &; i/ -)i*=-)(* tA n !o0:9!o01li3(3i2 l# !o0 :9!o01(3l#3i2; nd; nd; !ro" dur d !la#ar 1:3i:int ' r2; -ar (an:r al; 5:int ' r; \$ 'in i/ :=i tA n \$ 'in (an:9-)i*; /or 5:9I doCnto :H1 do -)5*:9-)561*; -):*:9(an; nd; nd; \$ 'in Crit 1E"at l ( nt ar - "torulK9D2;r adln1n2; /or i:91 to n do \$ 'in Crit 1Eta#tati l ( ntul E3i3D9D2;r adln1-)i*2; nd; /or i:9& to n do \$ 'in ::9!o0113i613i2; d !la#ar 1:3i2; nd; Crit ln1E- "torul ordonat #t :D2; /or i:91 to n do Crit ln1-)i*2; r adln; nd+

CONCLU'II
O

Al'orit(ii d ti! Di-id t I(! ra au \$una "o(!ortar in ti(! 3da"a # ind !lin #" ur(atoar l "onditii: di( n#iunil #u\$!ro'ra( lor1in "ar # i(!art !ro\$l (a initiala 2#unt a!ro,i(ati- 'al 1!rin"i!iul \$alan#arii2; li!# #" /a0 l d "o(\$inar a #olutiilor #u\$!ro\$l ( lor1"autar \$inara2+

1J