ΠΛΗ10 ΜΑΘΗΜΑ 2.7

Click here to load reader

  • date post

    14-Jan-2017
  • Category

    Education

  • view

    1.086
  • download

    0

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. Fibonacci2. 3. :

    , 10, 2.7:

    2

  • . 1.

    3 , 10, 2.7:

    !

    ( ) , ( ).

    , !

    : n :

    ! 1 2 2 1

    .. 1! 1, 2! 2 1, 3! 3 2 1 6, 4! 4 3 2 1 24 ... :

    ! 1 ! 1 ! 1, 1

  • . 1. 1.

    4 , 10, 2.7:

    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.

    5 , 10, 2.7:

    , .

    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 y2

    n

    fact(3)

    3

    y

    fact(2)

    n y2

    fact(1)

    n y1

  • . 1. 1.

    6 , 10, 2.7:

    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 y2

    fact(1)

    n y1 1

    1

    n

    fact(3)

    3

    y

    fact(2)

    n y2

    2

    1

    n

    fact(3)

    3

    y

    6

    2

    main

  • . 1. 1.

    7 , 10, 2.7:

    .

    :

    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)

    8 , 10, 2.7:

    (Binary Search).

    , .

    x :

    x, ! x

    x

  • . 1. 2. (Binary Search)

    9 , 10, 2.7:

    ( ) :

    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)

    10 , 10, 2.7:

    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

  • . 1. 2. (Binary Search)

    11 , 10, 2.7:

    : =[1 3 5 7 9] 7

    : ( Fibonacci ) .

  • . 2. 1. (Merge Sort)

    12 , 10, 2.7:

    , (Merge Sort).

    :

    .

  • . 2. 1. (Merge Sort)

    13 , 10, 2.7:

    :

    merge_sort(%PIN,start,finish)

    PIN: ARRAY[1..MAX_N] OF INTEGER;start,finish: INTEGER;

    PIN: ARRAY[1..MAX_N] OF INTEGER;

    middle: INTEGER;

    (startPIN[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);

    - -

    -

  • 14 , 10, 2.7:

    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)

  • 15 , 10, 2.7:

    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 19 10 11 12 13 14 15 16

    22 19 14 5 2 3 10 13

    . 2. 1. (Merge Sort)

  • 16 , 10, 2.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

    18 7 4 11 9 20 6 19 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)

  • 17 , 10, 2.7:

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

  • 18 , 10, 2.7:

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

  • 19 , 10, 2.7:

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

  • 20 , 10, 2.7:

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

  • 21 , 10, 2.7:

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

  • 22 , 10, 2.7:

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

  • 23 , 10, 2.7:

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

  • 24 , 10, 2.7:

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

  • 25 , 10, 2.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 209 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)

  • 26 , 10, 2.7:

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

  • 27 , 10, 2.7:

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

    28 , 10, 2.7:

    merge : .

    , .

    .

  • 29 , 10, 2.7:

  • . 2. 1. (Merge Sort)

    30 , 10, 2.7:

    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)

    31 , 10, 2.7:

    merge :