of 11

• date post

14-Jan-2017
• Category

## Education

• view

1.049

1

Embed Size (px)

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

• 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);

• . 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 & ! ' & "& /"

'& ' & "& #" #

• . 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. xA[middle]

-: BinarySearch(A,11,5,7) : middle=(5+7) div 2=6 x

• . 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

• - (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)

• - (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)

• - (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 # ' "'!.

• "

. 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

• . 2. # & 1. + \$" (Merge Sort)

+: - # ' ' ! " merge sort

' # ' #

'& =[6 2 4 1 8 10 11 12]

. 2. # & 2. . + (Quick Sort)

& + ' ( & ), &

! (Quick Sort).

& ) + :

'# # \$"& & ( \$"& "& ' & '

#)

3)&(