数据结构与算法(九) - · PDF filevoid fstream::open(char*name, openmode mode); //...

download 数据结构与算法(九) -   · PDF filevoid fstream::open(char*name, openmode mode); // ... •对外存设备上(文件)的排序技术

If you can't read please download the document

Transcript of 数据结构与算法(九) - · PDF filevoid fstream::open(char*name, openmode mode); //...

  • 2008. 6

    http://www.jpk.pku.edu.cn/pkujpk/course/sjjg

  • 2

    2

    9

    9.1

    9.2 9.2.1

    9.2.2 C++

    9.3

  • 3

    3

    ( primary memory main memory ) ( Random Access Memory, RAM )

    ( cache )

    ( video memory )

    ( peripheral storage secondary storage) (G - T 1012B )

    (P 1015B )

    9.1

  • 4

    4

    9.1

    ()

    ()

  • 5

    5

    9.1

  • 6

    6

    9.1

    bit

  • 7

    7

    9.1

    a b 3

    512 1024

  • 8

    8

    CPU

    9.1

  • 9

    9

    1 = 10-9

    1 = 10-3

    9.1

  • 10

    10

    KB (kilo byte) 103B ()

    MB (mega byte) 106B ()

    GB (giga) 109B ()

    TB (tera) 1012B ()

    PB (peta) 1015B ()

    EB = 1018BZB = 1021BYB = 1024B

    Googol 10 100

    9.1

  • 11

    11

    9.2

  • 12

    12

    ( logical file )

    ( physical file )

    9.2

  • 13

    13

    9.2

  • 14

    14

    9.2

  • 15

    15

    C++

    istream

    ostream

    iostream

    3 ifstream istream

    ofstream ostream

    fstream iostream

    9.2

  • 16

    16

    fstream

    #include // fstream = ifstream + ofstream

    void fstream::open(char*name, openmode mode);

    // fstream::read(char*ptr, int numbytes); // fstream::write(char*ptr, int numbtyes); //

    // seekgseekp//

    fstream::seekg(int pos); // fstream::seekg(int pos, ios::curr);

    fstream::seekp(int pos); // fstream::seekp(int pos, ios::end);

    void fstream::close(); //

    9.2

  • 17

    17

    ( buffering ) ( caching )

    ( page ) I/O

    ( buffer pool )

    9.2

  • 18

    18

    ( FIFO )

    ( LFU )

    ( LRU )

    9.2

  • 19

    19

    1.

    2. G

    ( rpm )

    9.2

  • 20

    20

    9

    9.1

    9.2

    9.3 9.3.1

    9.3.2

    9.3.3

  • 21

    21

    ()

    run

    9.3

  • 22

    22

    9.3

  • 23

    23

    9.3

    >16

    1629

    19 31

    25 21 56 40

    14

    35

    1629 12

    19

  • 24

    24

    9.3

    31>21

    21

    40>29>25

    35>31>25

    35>2913

    56

    56>31>29

    56>40>35

  • 25

    25

    // Elem // A n // in out template

    void replacementSelection(Elem * A, int n, const char * in, const char * out) {

    Elem mval; // Elem r; // FILE * inputFile; // FILE * outputFile;

    Buffer input; // bufferBuffer output; // initFiles(inputFile, outputFile, in, out);

    initMinHeapArry(inputFile, n, A); // MinHeap H(A, n, n);

    initInputBuffer(input, inputFile);

    9.3

  • 26

    26

    for(int last = (n-1); last >= 0;){

    mval = H.heapArray[0]; //

    sendToOutputBuffer(input, output,

    inputFile, outputFile, mval);

    input.read(r); //

    if (!less(r, mval))

    H.heapArray[0] = r; // r

    else { // lastrlast

    H.heapArray[0] = H.heapArray[last];

    H.heapArray[last] = r;

    H.setSize(last--);

    }

    H.SiftDown(0); //

    } // for

    endUp(output, inputFile, outputFile);

    }

    9.3

  • 27

    27

    M

    M

    2M

    9.3

  • 28

    28

    9.3

  • 29

    29

    (

    )

    Huffman

    9.3

  • 30

    30

    9.3

    1

    3

    18 4500

    123000

    6 1500

    1

    3

    750

    3*6 + 6*2 + 12 +6= 48

    1 2

    1 2 3 4 5 6

  • 31

    31

    9.3

  • 32

    32

    k k

    k k-1

    k

    m k log

    km k

    9.3

  • 33

    33

    B[3] B[2]

    L[1] L[2]

    B[1]

    B[4] B[5] L[5] L[6]

    L[3] L[4]

    nLowExt offset (+LowExt)L[i]B[p]ip

    p = LowExtinLowExti

    LowExtioffseti

    21

    2

    /)(

    /)({

    n=6, LowExt=4, Offset=7

    LowExt + Offset = 2n-1

  • 34

    34

    L[4]64 L[4]

    4

    4

    8

    5

    2 4

    5

    B[1]

    B[2] B[3]

    B[4] B[5] B[6] B[7]

    L[1] L[2] L[3] L[4] L[5] L[6] L[7] L[8]

    14

    26

    .

    .

    .

    22

    38

    .

    .

    .

    24

    30

    .

    .

    .

    15

    25

    .

    .

    .

    16

    50

    .

    .

    .

    11

    28

    .

    .

    .

    100

    110

    .

    .

    .

    18

    40

    .

    .

    .

    1 2 3 4 5 6 7 8

    10 9 20 8 12 90 17

    25

    .

    .

    .

    15

    6

    20>15

    4

    98

    5

  • 35

    35

    9.3

    n=6

    LowExt=4

    Offset=7

    LowExtinLowExti

    LowExtioffseti

    21

    2

    /)(

    /)({

    B[3] B[2]

    L[1] L[2]

    B[1]

    B[4] B[5] L[5] L[6]

    L[3] L[4]

    B[0]

  • 36

    36

    n

    9.3

    B[3] B[2]

    L[1] L[2]

    B[1]

    B[4] L[4] L[5]

    L[3]

    B[0]

  • 37

    37

    L[8] L[7] L[6] L[5] L[4] L[3] L[2] L[1]

    B[7] B[6] B[5] B[4]

    B[3] B[2]

    B[0]

    B[1] 5

    2

    7

    8

    1 3 6

    1 2 3 4 5 6 7 8

    10 9 20 12 90 17

    6

    15

    L[3]>L[4]

    3

    L[2]L[6]

    L[6]

  • 38

    38

    ADTtemplate

    class LoserTree{

    private:

    int MaxSize; // int n; // int LowExt; // int offset; // int * B; // T * L; // void Play(int p,int lc,int rc,int(*winner)(T A[],int b,int c));

    9.3

  • 39

    39

    ADT

    9.3

    public:

    LoserTree(int Treesize = MAX);

    ~LoserTree(){delete [] B;}

    void Initialize(T A[], int size,int (*winner)(T A[], int b, int c),

    int(*loser)(T A[], int b, int c)); // int Winner(); // void RePlay(int i, int(*winner)(T A[], int b, int c), int (*loser)(T A[], int

    b, int c)); // i };

    // Winner B[0] template

    int LoserTree::Winner(){

    return (n)?B[0]:0;

    }

  • 40

    40

    template // void LoserTree::Initialize(T A[], int size, int(*winner)(T A[], int

    b, int c), int(*loser)(T A[], int b, int c)) {

    if (size > MaxSize || size < 2) {

    cout

  • 41

    41

    template

    void LoserTree::Play(int p, int lc, int rc, int(* winner)(T

    A[], int b, int c), int(* loser)(T A[], int b, int c)){

    B[p] = loser(L, lc, rc); // B[p]int temp1, temp2;

    temp1 = winner(L, lc, rc);// pwhile(p>1 && p%2) { //

    temp2 = winner(L, temp1, B[p/2]);

    B[p/2] = loser(L, temp1, B[p/2]);

    temp1 = temp2;

    p/=2;

    } // B[p]B[1]B[p/2] = temp1;

    }

    9.3

    Play B[3] B[2]

    L[1] L[2]

    B[1]

    B[4] B[5] L[5] L[6]

    L[3] L[4]

    8

    10 9 20 6

    1

    2

    12

    6

    3

    5

    4B[0]

  • 42

    42

    RePlaytemplate void LoserTree::RePlay(int i, int

    (*winner)(T A[], int b, int c), int (*loser)(T A[], int b, int c)){

    if (i n) {

    cout

  • 43

    43

    m k log

    km

    m

    k

    9.3

  • 44

    44

    k n

    (kn)(k)

    (k+nlog k)

    k (k)

    (log k)

    9.3

  • 45

    45

    9.3

    13

    2

    14

    7 6

    15 8 10 9

    27 42 25

    94

    6 25 13

    44

    8 2 9

    19

    14 10 7

    31

    94

    (a) (b)

    376 356

  • 46

    46

    9.3

  • http://www.jpk.pku.edu.cn/pkujpk/course/sjjg/

    2008. 6