Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ΠΛΗ10 ΜΑΘΗΜΑ 2.7 (ΕΚΤΥΠΩΣΗ)

2,401 views

Published on

A. Αναδρομή
1) Αναδρομικές Συναρτήσεις
1.1) Υπολογισμός Παραγοντικού
1.2) Δυαδική Αναζήτηση (Binary Search)
2) Αναδρομικές Διαδικασίες
2.1) Ταξινόμηση με Συγχώνευση (Merge Sort)
2.2) Γρήγορη Ταξινόμηση (Quick Sort)
Β. Ασκήσεις
1) Οι αριθμοί Fibonacci
2) Υπολογισμός ΜΚΔ με τον αλγόριθμο του Ευκλείδη
3) Πρόγραμμα: Ταξινόμηση Πίνακα

Published in: Education
  • Be the first to comment

  • Be the first to like this

ΠΛΗ10 ΜΑΘΗΜΑ 2.7 (ΕΚΤΥΠΩΣΗ)

  1. 1. 10 2: 2.7: ! A. 1. 1. 2. (Binary Search) 2. 1. (Merge Sort) 2. ! (Quick Sort) ". 1. # $ Fibonacci 2. %& $ ' 3. : " . 1. # $ ( ) ! % ( & ) ' & , ( ) ( & " ). & & ' ! " , &) ( ' ' &( ! ! # ' : ' * ! ! n &( ) : '.". #" . . . ' &( ) ) + : , . 1. # $ 1. ,' ! factorial n,res: INTEGER; factorial(n): INTEGER n: INTEGER; factorial: INTEGER; y:INTEGER; (n=1) factorial:=1; y:=factorial(n-1); factorial:=n*y; - - (EOLN, " n: "); (n); res:=factorial(n); (n,"!=",res);
  2. 2. . 1. # $ 1. ,' ! $ ' & , #" " . ! ') #" factorial(3): - & factorial(2): - & factorial(1): factorial(n): INTEGER (n=1) factorial:=1; y:=factorial(n-1); factorial:=n*y; - - n … * fact(3) 3 y n * fact(3) 3 y * fact(2) n y 2 n * fact(3) 3 y * fact(2) n y 2 * fact(1) n y 1 . 1. # $ 1. ,' ! H factorial(1) ' #* 1: factorial(2) ' ! 1 y #' ' #* 2*1. factorial(2) ' ! 1 y #' ' #* 3*2 factorial(n): INTEGER (n=1) factorial:=1; y:=factorial(n-1); factorial:=n*y; - - n * fact(3) 3 y * fact(2) n y 2 * fact(1) n y 1 1 1 n * fact(3) 3 y * fact(2) n y 2 2 1 n * fact(3) 3 y 6 2 main . 1. # $ 1. ,' ! . ' & # ' & ' & ' ' "&( & + . ' ' ! ' ' # ! : factorial(n): INTEGER (n=1) factorial:=1; y:=factorial(n-1); factorial:=n*y; - - factorial(3) (3=1) y:=factorial(2) factorial(2) (2=1) y:=factorial(1) factorial(1) (1=1) 1 y=1 2*1=2 y=2 3*2=6 . 1. # $ 2. ( (Binary Search) ! - ' & ' ' & ' & & (Binary Search). ' & ' & ' / '& , ' & ') & . $ ' ( " & x # + # !+ '& : & " & & x, " & 0 # ! x & ' & " & / " '& ' " #" & " & x & ! ' & " & / " '& ' & " & #" #
  3. 3. . 1. # $ 2. ( (Binary Search) " & ' & / & ( ) & : binary_search(PIN,x,start,finish): BOOLEAN … check:BOOLEAN; middle:INTEGER; (start>finish) check:=FALSE; middle:=(start+finish) DIV 2; (x=PIN[middle]) check:=TRUE; (x>PIN[middle]) check:=binary_search(PIN,x,middle+1,finish); check:=binary_search(PIN,x,start,middle-1); - - - binary_search:=check; - . 1. # $ 2. ( (Binary Search) ! / " " & 11 '& : - : BinarySearch(A,11,1,15): middle=(1+15) div 2=8. x<A[middle] - : BinarySearch(A,11,1,7) : middle=(1+7) div 2=4 x>A[middle] - : BinarySearch(A,11,5,7) : middle=(5+7) div 2=6 x<A[middle] - : BinarySearch(A,11,5,5) : middle=(5+5) div 2=5 x=A[middle] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 3 5 7 11 13 17 21 23 27 31 33 37 41 43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 3 5 7 11 13 17 21 23 27 31 33 37 41 43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 3 5 7 11 13 17 21 23 27 31 33 37 41 43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 3 5 7 11 13 17 21 23 27 31 33 37 41 43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 3 5 7 11 13 17 21 23 27 31 33 37 41 43 start start start start=finish finish finish finish . 1. # $ 2. ( (Binary Search) + : - # ' ' ! " ( ' # ' # '& =[1 3 5 7 9] ( " & 7 $ &) : $ & & ' ! * # ( & Fibonacci & ) #' ' ") ) & . . 2. # & 1. + $ " (Merge Sort) 1 + ' & , & (Merge Sort). & ) + : + & '& + & +& '& $ ") ! ! + # ' # & + # & + ! & & & .
  4. 4. . 2. # & 1. + $ " (Merge Sort) ' & / & : merge_sort(%PIN,start,finish) PIN: ARRAY[1..MAX_N] OF INTEGER; start,finish: INTEGER; PIN: ARRAY[1..MAX_N] OF INTEGER; middle: INTEGER; (start<finish) (start+1=finish) /* 2 ! "# */ (PIN[start]>PIN[finish]) swap(%PIN[start],%PIN[finish]); - /* $ # # $ 2 ! "# */ middle:=(start+finish) DIV 2; merge_sort(%PIN,start,middle); merge_sort(%PIN,middle+1,finish); merge(%PIN,start,finish); - - - - MergeSort( ,1,16) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 9 10 13 . 2. # & 1. + $ " (Merge Sort) - MergeSort( ,1,16) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 . 2. # & 1. + $ " (Merge Sort) - (A,1,8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 18 7 4 11 5 6 7 8 9 20 6 1 . 2. # & 1. + $ " (Merge Sort)
  5. 5. - (A,1,4) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 18 7 4 11 5 6 7 8 9 20 6 1 1 2 18 7 3 4 4 11 . 2. # & 1. + $ " (Merge Sort) ! - (A,1,2): + ' '& 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 18 7 4 11 5 6 7 8 9 20 6 1 1 2 7 18 3 4 4 11 . 2. # & 1. + $ " (Merge Sort) " - (A,3,4): + ' '& 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 18 7 4 11 5 6 7 8 9 20 6 1 1 2 7 18 3 4 4 11 . 2. # & 1. + $ " (Merge Sort) - (A,1,4): $ " ) ! ' ' ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 4 7 11 18 5 6 7 8 9 20 6 1 1 2 7 18 3 4 4 11 . 2. # & 1. + $ " (Merge Sort)
  6. 6. - (A,5,8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 4 7 11 18 5 6 7 8 9 20 6 1 1 2 7 18 3 4 4 11 5 6 9 20 7 8 6 1 . 2. # & 1. + $ " (Merge Sort) - (A,5,6): + ' '& 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 4 7 11 18 5 6 7 8 9 20 6 1 1 2 7 18 3 4 4 11 5 6 9 20 7 8 6 1 . 2. # & 1. + $ " (Merge Sort) - (A,7,8): + ' '& 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 4 7 11 18 5 6 7 8 9 20 6 1 1 2 7 18 3 4 4 11 5 6 9 20 7 8 1 6 . 2. # & 1. + $ " (Merge Sort) - (A,5,8): $ " ) ! ' ' ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 18 7 4 11 9 20 6 1 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 4 7 11 18 5 6 7 8 1 6 9 20 1 2 7 18 3 4 4 11 5 6 9 20 7 8 1 6 . 2. # & 1. + $ " (Merge Sort)
  7. 7. - (A,1,8): $ " ) ! ' ' ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 1 4 6 7 9 11 18 20 9 10 11 12 13 14 15 16 22 19 14 5 2 3 10 13 1 2 3 4 4 7 11 18 5 6 7 8 1 6 9 20 1 2 7 18 3 4 4 11 5 6 9 20 7 8 1 6 . 2. # & 1. + $ " (Merge Sort) & " & # (9,16) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 7 4 11 9 20 6 1 22 19 14 5 2 3 10 13 1 2 3 4 5 6 7 8 1 4 6 7 9 11 18 20 9 10 11 12 13 14 15 16 2 3 5 10 13 14 19 22 1 2 3 4 4 7 11 18 5 6 7 8 1 6 9 20 1 2 7 18 3 4 4 11 5 6 9 20 7 8 1 6 9 10 11 12 5 14 19 22 13 14 15 16 2 3 10 13 9 10 19 22 11 12 5 14 13 14 2 3 15 16 10 13 . 2. # & 1. + $ " (Merge Sort) - (A,1,16): $ " ) ! ' ' ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 9 10 11 13 14 18 19 20 22 1 2 3 4 5 6 7 8 1 4 6 7 9 11 18 20 9 10 11 12 13 14 15 16 2 3 5 10 13 14 19 22 1 2 3 4 4 7 11 18 5 6 7 8 1 6 9 20 1 2 7 18 3 4 4 11 5 6 9 20 7 8 1 6 9 10 11 12 5 14 19 22 13 14 15 16 2 3 10 13 9 10 19 22 11 12 5 14 13 14 2 3 15 16 10 13 . 2. # & 1. + $ " (Merge Sort) . 2. # & 1. + $ " (Merge Sort) ! & merge ! ) + : $ ! '& " ' ' + . $ & ! #" " & ) ' ) ' # 0 ( ' # + # & 2 + ! " & # ' ! '& , 0 ( " & '# ' '& # + # & . $ ' 0 #' 0 # ' " ' ! .
  8. 8. " . 2. # & 1. + $ " (Merge Sort) & ' & merge & : merge(%PIN,start,finish) PIN: ARRAY[1..MAX_N] OF INTEGER; start,finish: INTEGER; PIN: ARRAY[1..MAX_N] OF INTEGER; C: ARRAY[1..MAX_N] OF INTEGER; middle: INTEGER; i,j,k:INTEGER; n: INTEGER; m: INTEGER; middle:=(start+finish) DIV 2; /* 1 % "&#'#% PIN[start...middle]*/ i:=start; n:=middle; /* 2 % "&#'#% PIN[middle+1...finish]*/ j:=middle+1; m:=finish; /* C: ()!*& (+ & % "&#'#% */ k:=1; ( (& !", # …) . 2. # & 1. + $ " (Merge Sort) & ' & merge & : ( (& ! #…) /* 1. ()! & ( - *& ./ &0'*& */ (i<=n AND j<=m) (PIN[i]<PIN[j]) C[k]:=PIN[i]; k:=k+1; i:=i+1; C[k]:=PIN[j]; k:=k+1; j:=j+1; - - ( (& !", # …) . 2. # & 1. + $ " (Merge Sort) /* 2. & ) # 1 ( "&#'# ( « / 2 % ( ()!*& (+ & ( "&#'# */ (i=n+1) /* 3#& 214-' 1 % "&#'#% */ (j<=m) C[k]:=PIN[j]; k:=k+1; j:=j+1; - /* 3#& 214-' 2 % "&#'#% */ (i<=n) C[k]:=PIN[i]; k:=k+1; i:=i+1; - - /* 3. & ) # 1 ( C & PIN */ k:=1; i:=start; (i<=finish) PIN[i]:=C[k]; i:=i+1; k:=k+1; - -
  9. 9. . 2. # & 1. + $ " (Merge Sort) + : - # ' ' ! " merge sort ' # ' # '& =[6 2 4 1 8 10 11 12] . 2. # & 2. . + (Quick Sort) & + ' ( & ), & ! (Quick Sort). & ) + : ' # # $ " & & ( $ " & – " & ' & ' # ) 3) &( '& ! # : " & ' & ! " & " & ' & ! & ! " & ' 0 ! ' '& ' ' # / . . 2. # & 2. . + (Quick Sort) ' & / & : quick_sort(%PIN,start,finish) PIN: ARRAY[1..MAX_N] OF INTEGER; start,finish: INTEGER; PIN: ARRAY[1..MAX_N] OF INTEGER; (start<finish) pos=partition(%PIN,start,finish); /* #+ - ( "&#'# */ quick_sort(%PIN,start,pos); /* &#. +1 & # $ ( "&#'# */ quick_sort(%PIN,pos+1,finish);/* &#. +1 & . 3 $ ( "&#'# */ - - + QuickSort (2. # ) 1 2 3 4 5 6 7 8 9 10 18 7 4 11 9 20 6 1 22 19 1 7 4 11 9 6 20 18 22 19 1 2 3 4 5 6 1 7 4 11 9 6 1 7 4 11 9 6 7 8 9 10 20 18 22 19 19 18 22 20 2 3 4 5 6 7 4 11 9 6 6 4 11 9 7 2 3 6 4 4 6 4 5 6 11 9 7 7 9 11 2 4 4 3 6 6 4 5 11 9 7 9 6 11 11 4 7 7 5 9 9 1 1 1 1 1 2 4 3 6 4 7 5 9 6 11 7 18 7 8 19 18 18 19 9 10 22 20 20 22 7 18 18 8 19 19 9 20 20 10 22 22 8 19 9 20 10 22 8 19
  10. 10. . 2. # & 2. . + (Quick Sort) # ) " &) ! & ' & & ' ! ' : .". # ' ' & " ' # 0 '& ' 0 ( " & + ! " & !. 4 & # ' # , ' *# ) " Hoare: $ '& ' / " # " & ' & ! ( & ) ! $ '& ' + / " # " & ' & ( & ) ! ! " & ' 0 #" & ") ) " &) . 5 " Hoare ' & ( ' * ) 1 2 6 4 4 6 . 2. # & 2. . + (Quick Sort) ! # partition: & # 2 " & : 1 " : 1 2 3 4 5 6 7 8 9 10 18 7 4 11 9 20 6 1 22 19 i=1 j=8 1 2 3 4 5 6 7 8 9 10 18 7 4 11 9 20 6 1 22 19 1 18 22 19 1 2 3 4 5 6 7 8 9 10 18 7 4 11 9 20 6 1 22 19 1 18 22 19i=6 j=7 1 2 3 4 5 6 7 8 9 10 1 7 4 11 8 6 20 18 22 19 1 2 3 4 5 6 7 8 9 10 18 7 4 11 9 20 6 1 22 19 1 7 4 11 8 6 20 18 22 19 i=7j=6 1 2 3 4 5 6 7 8 9 10 1 7 4 11 8 6 20 18 22 19 1 2 6 4 i=1 j=1 i=2 1 2 4 6 1 2 4 6 i=1 1 2 4 6 j=2 j=1 1 6 i=1 j=1 . 2. # & 2. . + (Quick Sort) " ' & partition " Hoare / & : partition(%PIN,start,finish):INTEGER PIN: ARRAY[1..MAX_N] OF INTEGER; start,finish: INTEGER; PIN: ARRAY[1..MAX_N] OF INTEGER; partition: INTEGER; pivot,i,j: INTEGER; stop: BOOLEAN; pivot:=PIN[start]; i:=start-1; j:=finish+1; stop:=FALSE; (stop=FALSE) j:=j-1; (PIN[j]<=pivot) i:=i+1; (PIN[i]>=pivot) (i<j) swap(%PIN[i],%PIN[j]); stop:=TRUE; partition:=j; - - - . 2. # & 2. . + (Quick Sort) + : - # ' ' ! " merge sort ' # ' # '& =[6 2 4 1 8 10 11 12]
  11. 11. 6. * 1 ( : & FIbonacci) & fibonacci &( ) : Fn=Fn-1+Fn-2, n>2 F2=1 F1=1 . ' #" F1=1,F2=1,F3=2,F4=3,F5=5,F6=8 . . . & fibonacci(n) ' #" ) # * ' #* n- fibonacci. 1' # ' 0 ( ' " # # ' &( ' #* fibonacci ! ' " . 6. * 2 ( : - & ) O & ! # - ! # ! (* ) : 7 # ( ! * " &( # # ( * ' ! . & ' 0 ) & & & . & - ) " . - (a,b) ' a,b & * &: & & a, a=b & & - (a,b-a), a<b & & - (a-b,b), - # ' ' & & ' - ( ' " ! * ! , ' - ) . 6. * 3 ( : + & ) ' & ' + ' ) ) & + " (Merge Sort) + (Quick Sort).

×