Example #1
0
void printLocalMax(const int *arr, size_t len, size_t window) {
  MaxHeap<ValueWrapper> h;
  for (int i = 0; i < window - 1; ++i) {
    h.add(ValueWrapper(arr[i], i));
  }
  for (int i = window - 1; i < len; ++i) {
    h.add(ValueWrapper(arr[i], i));
    for(;;) {
      ValueWrapper *r = h.top();
      if (i - r->pos < window) {
	cout << r->value << endl;
	break;
      }
      h.pop();
    }
  }
}
double greedyKnapsack(knapsack &k){
	MaxHeap<float, int>* heap = new MaxHeap<float, int>(&compare);
	for(int counter = 0; counter < k.getNumObjects(); counter++){
		double key = (double) k.getValue(counter) / k.getCost(counter);
		heap->add(key, counter);
	}
	while(! heap->empty()){
		int next = heap->extractMaxHeapMaximum();
		if(k.getCost(next) + k.getCurrentCost() <= k.getCostBound()){
			k.select(next);
		}
	}
	return k.getCurrentValue();
}
double testHeap(T testData[], int n, bool isHeapify) {
    clock_t startTime = clock();
    MaxHeap<T> *maxHeap;
    if (isHeapify) {
        maxHeap = new MaxHeap<T>(testData, n);
    } else {
        maxHeap = new MaxHeap<T>();
        for (int i = 0; i < n; ++i) {
            maxHeap->add(testData[i]);
        }
    }

    T *arr = new T[n];
    for (int j = 0; j < n; ++j) {
        arr[j] = maxHeap->extractMax();
    }

    for (int k = 1; k < n; ++k) {
        assert(arr[k - 1] >= arr[k]);
    }
    std::cout << "Test MaxHeap completed." << std::endl;
    clock_t endTime = clock();
    return double(endTime - startTime) / CLOCKS_PER_SEC;
}