summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/heapify.cc
blob: bb193601c1c07fec728f3a5d7c086975a511c22d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

enum class HeapType {
    Min,
    Max
};

template <typename H>
class Heap {
public:
    Heap(HeapType type, int n)
        : n_ { n + 1 }
        , type_ { type }
    {
        a_ = new H[n + 1];
        a_[0] = 0;
    }

    ~Heap()
    {
        delete[] a_;
    }

    void
    heapify(int i)
    {
        if (heapsize_ >= 1)
            count_++;

        int l = left(i);
        int r = right(i);
        int max = i;

        if (l <= heapsize_ && compare(l, max))
            max = l;
        if (r <= heapsize_ && compare(r, max))
            max = r;

        if (max != i) {
            swap(a_[i], a_[max]);
            heapify(max);
        }
    }

    void
    build(H aux[], int n)
    {
        a_ = aux;
        heapsize_ = n;

        for (int i = heapsize_ / 2; i > 0; --i) {
            heapify(i);
        }
    }

    H
    extract()
    {
        swap(a_[1], a_[heapsize_]);
        heapsize_--;
        heapify(1);

        return a_[heapsize_ + 1];
    }

    inline int
    left(int i)
    {
        return i << 1;
    }
    inline int
    right(int i)
    {
        return (i << 1) | 1;
    }

    void
    sort(ostream& out)
    {
        H* b = new H[heapsize_ + 1];

        int n = heapsize_;

        for (unsigned i = n; i > 0; --i) {
            b[i] = extract();
        }
        out << count_ << endl;

        delete[] b;
    }

private:
    H* a_;
    int heapsize_ { 0 };
    int n_;
    HeapType type_;

    bool
    compare(int i, int j)
    {
        return (type_ == HeapType::Max) ? a_[i] > a_[j] : a_[j] > a_[i];
    }
    int count_ { 0 };
};

int
main(int argc, char** argv)
{
    int n = (argc > 1) ? stoi(argv[1]) : 100;
    ifstream fin("input.txt");
    ofstream fout("output.txt");

    string t;
    int m;

    for (int i = 0; i < n; ++i) {
        fin >> t >> m;

        if (t == "int") {
            Heap<int>* pq = new Heap<int>(HeapType::Max, m);
            int* k = new int[m + 1];

            k[0] = 0;
            for (int j = 1; j <= m; ++j) {
                fin >> k[j];
            }
            pq->build(k, m);

            pq->sort(fout);

            delete pq;
        } else if (t == "double") {
            Heap<double>* pq = new Heap<double>(HeapType::Max, m);
            double* k = new double[m + 1];

            k[0] = 0;
            for (int j = 1; j <= m; ++j) {
                fin >> k[j];
            }
            pq->build(k, m);

            pq->sort(fout);

            delete pq;
        } else if (t == "bool") {
            Heap<bool>* pq = new Heap<bool>(HeapType::Max, m);
            bool* k = new bool[m + 1];

            k[0] = 0;
            for (int j = 1; j <= m; ++j) {
                fin >> k[j];
            }
            pq->build(k, m);

            pq->sort(fout);

            delete pq;
        } else if (t == "char") {
            Heap<char>* pq = new Heap<char>(HeapType::Max, m);
            char* k = new char[m + 1];

            k[0] = 0;
            for (int j = 1; j <= m; ++j) {
                fin >> k[j];
            }
            pq->build(k, m);

            pq->sort(fout);

            delete pq;
        }
    }

    fin.close();
    fout.close();

    return 0;
}