Beispiel #1
0
void heap_sort(int * arr, int length, int asce) {
    int i;
    for (i = length/2; i >= 0; i--)
        min_heap(arr, i, length);
    int min;
    for (i = length-1; i >= 1; i--) {
        min = arr[0];
        arr[0] = arr[i];
        arr[i] = min;
        min_heap(arr, 0, i);
    }
}
Beispiel #2
0
void delete_min()
{
	min_heap();
	a[1]=a[hsize];
	hsize--;
	perc_down(1);
}
Beispiel #3
0
int main()
{
	int limit = std::numeric_limits<int>::min();
	Min_heap<int> min_heap(27, limit);
	min_heap.insert(13);
	min_heap.insert(21);
	min_heap.insert(16);
	min_heap.insert(24);
	min_heap.insert(31);
	min_heap.insert(19);
	min_heap.insert(68);
	min_heap.insert(65);
	min_heap.insert(26);
	min_heap.insert(32);
	min_heap.print(std::cout);
	std::cout<<"insert \"14\"\n";
	min_heap.insert(14);
	min_heap.print(std::cout);
	std::cout<<"erase minimal element\n";
	min_heap.erase_min();
	min_heap.print(std::cout);
	std::cout<<"decrease_key(5,20) : \n";
	min_heap.decrease_key(5,20);
	min_heap.print(std::cout);
	std::cout<<"increase_key(1,20) : \n";
	min_heap.increase_key(1,20);
	min_heap.print(std::cout);
	return 0;
}
Beispiel #4
0
int main(int argc, char *argv[])
{
	Heap min_heap(argv[1]); // heap.cpp
	Node* huffman_code_tree = min_heap.BuildHuffmanCodeTree(); // heap.cpp
	HuffmanCodeTreeTraversals(huffman_code_tree); // tree_traversal.cpp
	CodeTable code_table(huffman_code_tree); // code_table.cpp
	code_table.Encode(argv[1]); // code_table.cpp
}
Beispiel #5
0
int pop()
{
	int min = heap[1];
	heap[1] = heap[heap[0]];
	pos[heap[1]] = 1;
	heap[0]--;
	min_heap(1);
	return min;
}
 int findKthLargest(vector<int>& nums, int k) {
     MinHeap min_heap (k);
     for (int i = 0; i < nums.size(); ++i) {
         if (nums[i] > min_heap.getFirstValue()) {
             min_heap.replace(nums[i]);
         }
         // min_heap.print();
     }
     return min_heap.getFirstValue();
 }
Beispiel #7
0
void min_heap(int i)
{
	int min;
	int tmp;
	int r = RIGHT(i);
	int l = LEFT(i);
	if(l <= heap[0] && d[heap[l]] < d[heap[i]]) min = l;
	else min = i;
	if(r <= heap[0] && d[heap[r]] < d[heap[min]]) min = r;
	if(min != i)
	{
		tmp = heap[i]; heap[i] = heap[min]; heap[min] = tmp;
		min_heap(min);
		pos[heap[i]] = i;
		pos[heap[min]] = min;
	}
}
Beispiel #8
0
int FindMedian(std::vector<int> &vec) {
    std::vector<int> min_heap(1, INT_MAX), max_heap(1, INT_MIN);
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i] < max_heap[0]) {
            if (max_heap.size() > min_heap.size()) {
                int root = PopHeapRoot(max_heap, true);
                InsertHeap(min_heap, root, false);
            }
            InsertHeap(max_heap, vec[i], true);
        } else if (vec[i] > min_heap[0]) {
            if (min_heap.size() >= max_heap.size()) {
                int root = PopHeapRoot(min_heap, false);
                InsertHeap(max_heap, root, true);
            }
            InsertHeap(min_heap, vec[i], false);
        } else {
            if (min_heap.size() >= max_heap.size())
                InsertHeap(max_heap, vec[i], true);
            else
                InsertHeap(min_heap, vec[i], false);
        }
    }
    return max_heap[0];
}
Beispiel #9
0
std::unordered_map<int, std::unordered_map<int, int>> base_balance_algo_for_all(
    std::unordered_map<int, int>& num_objs) {
    int average = (int) std::accumulate(
        num_objs.begin(), num_objs.end(), 0.0,
        [&](double a, std::pair<int, int> pair) -> double { return a + (double) pair.second / num_objs.size(); });

    auto greater = [](const std::pair<int, int>& left, const std::pair<int, int>& right) {
        return left.second < right.second;
    };

    auto less = [](const std::pair<int, int>& left, const std::pair<int, int>& right) {
        return left.second > right.second;
    };

    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, decltype(greater)> max_heap(greater);
    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, decltype(less)> min_heap(less);

    // the minimum number of objects a thread should have
    int at_least = average;
    // the maximum number of objects a thread should have
    int at_most = average + 1;

    for (auto& v : num_objs) {
        if (v.second > at_least) {
            max_heap.push(v);
        }
        if (v.second < at_most) {
            min_heap.push(v);
        }
    }

    // the migration plan
    // key: source global_tid
    // value: destination global_tid and number of objects transfered
    std::unordered_map<int, std::unordered_map<int, int>> res;
    while (!min_heap.empty() && !max_heap.empty()) {
        std::pair<int, int> min_pair = min_heap.top();
        min_heap.pop();
        const int dst_tid = min_pair.first;
        const int min_obj_num = min_pair.second;
        int already_moved = 0;
        while (!max_heap.empty() && min_obj_num + already_moved < at_least) {
            int num_to_be_moved = 0;
            std::pair<int, int> max_pair = max_heap.top();
            max_heap.pop();
            const int src_tid = max_pair.first;
            const int max_obj_num = max_pair.second;
            const int at_most_can_move = max_obj_num - at_least;
            const int need = at_least - (min_obj_num + already_moved);
            // check whether this thread has enough objects to be moved
            // if not
            if (need >= at_most_can_move) {
                already_moved += at_most_can_move;
                num_to_be_moved = at_most_can_move;
            } else {
                already_moved += need;
                num_to_be_moved = need;
                // if it still has excess objects can be moved
                // push back to the heap
                if ((max_obj_num - num_to_be_moved) >= at_most) {
                    max_heap.push(std::make_pair(src_tid, max_obj_num - num_to_be_moved));
                }
            }
            assert(num_to_be_moved >= 0);
            if (res.find(src_tid) == res.end()) {
                res[src_tid] = std::unordered_map<int, int>();
            }
            res[src_tid][dst_tid] = num_to_be_moved;
        }
    }

    return res;
}