Example #1
0
void heapsort::sort(int arr[], int num)
{
    print(arr, num);

    int i = 0;
    for (i = (num / 2); i >= 0; --i) {
        percdown(arr, i, num);
        print(arr, num);
    }

    for (i = (num - 1); i > 0; --i) {
        swap1(&arr[0], &arr[i]);
        print(arr, num);
        percdown(arr, 0, i);
    }
}
Example #2
0
File: heap.c Project: chameco/gwir
void *pop_heap(heap *h) {
	if (h->size == 0) return NULL;
	void *ret = h->buffer[0];
	if (h->size == 1) destroy_heap(h);
	else { h->buffer[0] = h->buffer[--h->size]; percdown(h, 0); }
	return ret;
}
Example #3
0
//Deletes node with minimum valued key, removes from mapping
//Returns 1 if heap is empty, returns 0 on success
//Replaces deleted node space with node at the end, percolates it down
int heap::deleteMin(std::string *pId, int *pKey, void *ppData) {
	
	if (size == 0)
		return 1;
	
	if (pId != NULL)
		*pId = data[1].id;
	if (pKey != NULL)
		*pKey = data[1].key;
	if (ppData != NULL)
		*(static_cast<void **> (ppData)) = data[1].pData;
	
	mapping->remove(data[1].id);
	
	if (size == 1) {
		--size;
	}
	else {
		data[1] = data[size--];
		percdown(1);
	}
	
	++capacity;
	
	return 0;
}
Example #4
0
//Sets key of given id
//Returns 1 if id is not in heap
//Returns 0 on succcess
//Percolates up/down depending on new key's size compared to old key
int heap::setKey(const std::string &id, int key) {
	node* keynode = static_cast<node *> (mapping->getPointer(id));
	
	if (keynode == NULL)
		return 1;
	else {
		int tmpkey = keynode->key;
		keynode->key = key;
		if (key < tmpkey)
			percup(getPos(keynode));
		else if (key > tmpkey) {
			percdown(getPos(keynode));
		}
		return 0;
	}
}
Example #5
0
//Deletes node with given id, removes from mapping
//Returns 1 given id does not exist in heap, returns 0 on success
//Replaces deleted node space with node at the end, percolates it up/down
int heap::remove(const std::string &id, int *pKey, void *ppData) {

	node* tmpnode = static_cast<node *> (mapping->getPointer(id));
	
	if (tmpnode == NULL)
		return 1;
	
	int hole = getPos(tmpnode);
	
	if (pKey != NULL)
		*pKey = data[hole].key;
	if (ppData != NULL)
		*(static_cast<void **> (ppData)) = data[hole].pData;
	
	mapping->remove(data[hole].id);
	
	data[hole] = data[size--];
	percdown(hole);
	percup(hole);
	++capacity;
	
	return 0;
}