Пример #1
0
node *buildHeap(struct stack *s, int(*lt)(int, int))
{
    node *g = top(s);
    treeNode *b = g->treeN->parent;
    while(g->treeN->parent != NULL)
    {
        b = g->treeN->parent;
        if(b->right != NULL) //it's a right child
        {
            siftDown(b, lt);
            if(g->next->next != NULL)
            {
                g = g->next->next;
            }
        }
        else    //it's a left child
        {
            siftDown(b, lt);
            if(g->next != NULL)
            {
                g = g->next;
            }
        }
    }
    return g;
}
Пример #2
0
void hsort64(U128T records[], size_t count)
{
	int64_t start, end;
	U128T temp;

	status = 0;
	puts("Heapifying records");
	for (start=count/2-1; start>=0; start--) {
		if (bailout)
			exit(1);
		siftDown(records, start, count);
		status = count - 2*start;
	}

	status = 0;
	puts("Sorting records");
	for (end=count-1; end>0; end--) {
		if (bailout)
			exit(1);
		temp = records[end];
		records[end] = records[0];
		records[0] = temp;
		siftDown(records, 0, end);
		status = count - end + 1;
	}
}
void heapSort(int *a, int count) {
        int start, end;
        for (start = (count-2)/2; start >=0; start--) {
                siftDown( a, start, count);
        }//for

        for (end=count-1; end > 0; end--) {
                SWAP(a[end],a[0]);
                siftDown(a, 0, end);
        }//for
}//heapSort
Пример #4
0
void heapSort(void* buf, size_t size, size_t total, int (*compare)(void const* a, void const* b)) {
	int start, end;

	for(start = (total - 2) / 2; start >= 0; start--) {
		siftDown(buf, size, start, total, compare);
	}

	for(end = total - 1; end > 0; end--) {
		exch(buf, end, 0, size);
		siftDown(buf, size, 0, end, compare);
	}
}
Пример #5
0
void heapSortHelper (int* a, int first, int last)
{
    for (int i = last / 2; i >= first; i--)
    {
        siftDown(a, i, last);
    }
    for (int i = last; i > first; i--)
    {
        Sort::swap (a[i], a[first]);
        siftDown (a, first, i - 1);
    }
}
Пример #6
0
void heap_sort(int a[], int n){
	int i, temp;

	for (i = (n / 2)-1; i >= 0; i--)
		siftDown(a, i, n);

	for (i = n-1; i >= 1; i--){
		temp = a[0];
		a[0] = a[i];
		a[i] = temp;
		siftDown(a, 0, i-1);
	}
}
Пример #7
0
void internal_heapsort(int *a, int count)
{
  int start, end;

  /* heapify */
  for (start = (count - 2) / 2; start >= 0; start--) {
    siftDown(a, start, count);
  }

  for (end = count - 1; end > 0; end--) {
    SWAP(a[end], a[0]);
    siftDown(a, 0, end);
  }
}
Пример #8
0
void heapSort(LONG_T* keys, LONG_T* auxKey1, WEIGHT_T* auxKey2, LONG_T n)
{
	LONG_T i;

	for (i = (n/2)-1; i >= 0; i--)
		siftDown(keys, auxKey1, auxKey2, i, n);

	for (i = n-1; i >= 1; i--) {
		swapL(&keys[0], &keys[i]);
                swapL(&auxKey1[0], &auxKey1[i]);
                swapW(&auxKey2[0], &auxKey2[i]);
		siftDown(keys, auxKey1, auxKey2, 0, i-1);
	}

}
Пример #9
0
void HeapSortAsc( int *I ) /* sort array I in ascending order using heap sort algorithm */ 
{
	int i, tmp;
	//Make a heap!
	//Heapify
	for (i = (size-2)/2; i >=0; i--)
		siftDown(I,i,size);
	for (i = size-1; i >= 1; i--){
		tmp = I[0];
		I[0] = I[i];
		I[i] = tmp;
		siftDown(I,0,i);
	}
	return;
}
Пример #10
0
void heapsort(long * numbers, long n, int isort)
{
  long i, temp;
 
  for (i = (n / 2); i >= 0; i--)
    siftDown(numbers, i, n - 1,isort);
 
  for (i = n-1; i >= 1; i--)
  {
    temp = numbers[0];
    numbers[0] = numbers[i];
    numbers[i] = temp;
    siftDown(numbers, 0, i-1,isort);
  }
}
Пример #11
0
void CStreamMerger::permute(const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
{
    // the tree structure: element p has children p*2+1 and p*2+2, or element c has parent (unsigned)(c-1)/2
    // the heap property: no element should be smaller than its parent
    // the dedup variant: if(dedup), the top of the heap should also not be equal to either child
    // the method: establish this by starting with the parent of the bottom element and working up to the top element, sifting each down to its correct place
    if (activeInputs >= 2)
        for(unsigned p = (activeInputs-2)/2; p > 0; --p)
            siftDown(p);

    if(dedup)
        siftDownDedupTop(seek, numFields, stepExtra);
    else
        siftDown(0);
}
Пример #12
0
void heapSort(int numbers[], int array_size)
{
  int i, temp;

  for (i = (array_size / 2)-1; i >= 0; i--)
    siftDown(numbers, i, array_size);

  for (i = array_size-1; i >= 1; i--)
  {
    temp = numbers[0];
    numbers[0] = numbers[i];
    numbers[i] = temp;
    siftDown(numbers, 0, i-1);
  }
}
Пример #13
0
void heapify(RandomAccessIterator first, RandomAccessIterator last) {
    RandomAccessIterator start = first + (last - first)/2 - 1;
    while (start >= first) {
        siftDown(first, start, last-1);
        --start;
    }
}
Пример #14
0
void siftDown(int numbers[], int root, int bottom) 
{
	int maxChild = root * 2 + 1;	
	
	// Find the biggest child
	if(maxChild < bottom) 
	{
		int otherChild = maxChild + 1;
		// Reversed for stability
		maxChild = (numbers[otherChild] > numbers[maxChild])?otherChild:maxChild;
	} 
	else 
	{
		// Don't overflow
		if(maxChild > bottom) 
			return;
	}	
	
	// If we have the correct ordering, we are done.
	if(numbers[root] >= numbers[maxChild])
		return;	
	
	// Swap
	int temp = numbers[root];
	numbers[root] = numbers[maxChild];
	numbers[maxChild] = temp;	
	
	// Tail queue recursion. Will be compiled as a loop with correct compiler switches.
	siftDown(numbers, maxChild, bottom);
}
Пример #15
0
void heapify( TreeNode ** node){
	TreeNode * p = *node;
	while( p != NULL){
		siftDown( &p);
		p=p->getPrev();
	}
}
Пример #16
0
void heapSort( TreeNode ** rt, TreeNode ** lt, std::vector<int> & vec){
	TreeNode * temp = *lt;
	TreeNode * tempParent;
	//TreeNode * temp2;

	while( temp != NULL){
		if( temp == *rt){
			vec.push_back( (*rt)->getValue() );
			break;
		}
		vec.push_back( (*rt)->getValue() );
		(*rt)->setValue( temp->getValue() );
		tempParent = temp->getParent();
		if( tempParent->getRight() == temp){
			tempParent->setRight(NULL);
		}
		if( tempParent->getLeft() == temp){
			tempParent->setLeft(NULL);
		}
		temp = temp->getPrev();
		if( temp!=NULL){
			temp->setNext( NULL);
		}
		
		//delete temp1;
		//temp1 = NULL;
		siftDown( rt);
	}
}
Пример #17
0
    void siftDown(T (&a)[N], int index) {//max-heap
      while (2*index + 1 < N) {
        int l = 2*index + 1;
        if (l + 1 < N && a[l+1] < a[l]) {
          ++l;
        }
        if (a[l] < a[index]) {
          std::swap(a[l], a[index]);
        }
        index = l;
      }
#if INTRO_ALGRO
      int l = 2*index; //index start from 1
      int r = 2*index + 1;
      int largest = -1;
      if (l <= N && a[l] > a[index]) {
        largest = l; 
      }
      else {
        largest = index;
      }
      if (r <= N && a[r] > a[largest]) {
        largest = r;
      }
      if (largest != index) {
        std::swap(a[largest], a[index]);
        siftDown(a, largest);
    }
#endif
  }
Пример #18
0
void siftDown(treeNode *front, int(*lt)(int, int))
{
    treeNode *left = front->left;
    treeNode *right = front->right;
    treeNode *smallestOrLargest = front;
    int indicator = 0;  //swap indicator
    if(front->left != NULL && lt(left->value, smallestOrLargest->value))    //if there's a left child and it's value compares with the other
    {
        smallestOrLargest = left;
        indicator = 1;
    }
    if(front->right != NULL && lt(right->value, smallestOrLargest->value))  //if there's a right child and it's value compares with the other
    {
        smallestOrLargest = right;
        indicator = 2;
    }
    if(indicator == 0)  //no swap was made
    {
        return;
    }
    if(smallestOrLargest->value != front->value)    //if the original smallest or largest moved to a new node, swap and recursively call siftDown
    {
        swap(&smallestOrLargest->value, &front->value); 
        siftDown(smallestOrLargest, lt);
    }
}
Пример #19
0
void buildHeap(int *a, int n)
{
    for(int i=(n-2)/2; i>=0; i--)
    {
        siftDown(a, n, i);
    }
}
Пример #20
0
void heapsort(int arr[], int array_size)
{
  int i, temp;
  for (i = (array_size / 2); i >= 0; i--)
    siftDown(arr, i, array_size - 1);			//Heapify

  for (i = array_size-1; i >= 1; i--)
  {
    // Swap
    temp = arr[0];
    arr[0] = arr[i];
    arr[i] = temp;

    siftDown(arr, 0, i-1);
  }
}
void HeapOrderedTree :: deleteMin(){
  if( !empty() ){
    swap( heap[1], heap[tail_index] );
    tail_index--;
    siftDown();
  }
}
 void drainHeap() {
     size_t const count = a.size(buf);
     for (ssize_t next = count; next > 1; next--) {
         a.swap(buf, next, 1);
         siftDown(1, next - 1);
     }
 }
Пример #23
0
		SteerLib::AStarPlannerNode pop() {
			SteerLib::AStarPlannerNode returnNode = heap.front();
			heap.front() = heap.back();
			heap.pop_back();
			siftDown(0);
			return returnNode;
		}
Пример #24
0
Fringe removeFringe(Fringe fringe, State *s) {
  /* Removes an element from the fringe, and returns it in s. 
   * Moreover, the new fringe is returned.
   */
  if (fringe.size < 1) {
    fprintf(stderr, "removeFringe(..): fatal error, empty fringe.\n");
    exit(EXIT_FAILURE);    
  }
  fringe.deleteCnt++;
  fringe.size--;
  switch (fringe.mode) {
  case LIFO: /* LIFO == STACK */
  case STACK:
    *s = fringe.states[fringe.size];
    break;
  case FIFO:
    *s = fringe.states[fringe.front++];
    fringe.front %= MAXF;
    break;
  case PRIO: /* PRIO == HEAP */
  case HEAP:
    *s = fringe.states[1];
    fringe.states[1] = fringe.states[fringe.size + 1];
    siftDown(fringe.states, fringe.size, 1);
    break;
  }
  return fringe;
}
Пример #25
0
void* BinaryHeap_extract_item(BinaryHeap h, int id){
  /* extract an item with ID out and delete it */
  void *item;
  int *id_to_pos = h->id_to_pos;
  int pos;

  if (id >= h->max_len) return NULL;

  pos = id_to_pos[id];

  if (pos < 0) return NULL;

  assert(pos < h->len);

  item = (h->heap)[pos];

  IntStack_push(h->id_stack, id);

  if (pos < h->len - 1){/* move the last item to occupy the position of extracted item */
    swap(h, pos, h->len - 1);
    (h->len)--;
    pos = siftUp(h, pos);
    pos = siftDown(h, pos);
  } else {
    (h->len)--;
  }
 
  (h->id_to_pos)[id] = -1;

  return item;

}
Пример #26
0
void inline heapSort(DATA_TYPE numbers[], const int array_size)
{
	int i;
	DATA_TYPE temp;

	for (i = (array_size / 2)-1; i >= 0; i--)
		siftDown(numbers, i, array_size);
	
	for (i = array_size-1; i >= 1; i--)
	{
		temp = numbers[0];
		numbers[0] = numbers[i];
		numbers[i] = temp;
		siftDown(numbers, 0, i-1);
	}
}
Пример #27
0
static int siftDown(BinaryHeap h, int nodePos){
  int childPos, childPos1, childPos2;

  void **heap = h->heap;


  childPos1 = ChildrenPos1(nodePos);
  childPos2 = ChildrenPos2(nodePos);
  if (childPos1 > h->len - 1) return nodePos;/* no child */
  if (childPos1 == h->len - 1) {
    childPos = childPos1;/* one child */
  } else {/* two child */
    if ((h->cmp)(heap[childPos1], heap[childPos2]) == 1){ /* pick the smaller of the two child */
      childPos = childPos2;
    } else {
      childPos = childPos1;
    }
  }

  if ((h->cmp)(heap[nodePos], heap[childPos]) == 1) {
    /* if larger than child, swap */
    swap(h, nodePos, childPos);
    nodePos = siftDown(h, childPos);
  } 

  return nodePos;
}
Пример #28
0
// Prim algorithm 
void Prim(Graph G, int s){
	//perform Prims's algorithm on G starting with vertex s

	int j, heap[MaxVertices + 1], heapLoc[MaxVertices + 1];
	G->vertex[s].cost = 0;
	for (j = 1; j <= G->numV; j++) heap[j] = heapLoc[j] = j;
	heap[1] = s; heap[s] = 1; heapLoc[s] = 1; heapLoc[1] = s;
	int heapSize = G->numV;
	while (heapSize > 0)
	{
		int u = heap[1];
		if (G->vertex[u].cost == Infinity) break;
		G->vertex[u].colour = Black;
		//reorganize heap after removing top item
		siftDown(G, heap[heapSize], heap, 1, heapSize - 1, heapLoc);
		GEdgePtr p = G->vertex[u].firstEdge;
		while (p != NULL)
		{
			if (G->vertex[p->child].colour == White && p->weight < G->vertex[p->child].cost)
			{
				G->vertex[p->child].cost = p->weight;
				G->vertex[p->child].parent = u;
				siftUp(G, heap, heapLoc[p->child], heapLoc);
			}
			p = p->nextEdge;
		}
		--heapSize;
	}// end while
	printMST(G);
}// end Prim's
Пример #29
0
    // Heapify starts at the last internal node of the tree: n/2.
    // While it hasn't reached the root, it calls siftdown()
    // which recursively compares the node values. The tree will
    // be a max heap when heapify() returns
    void heapify () {
        int *start = rootOf(end());

        while(start >= root()) {
            siftDown(start);
            --start;
        }
    }
Пример #30
0
void heapsort() {
//	printf("heapsort start\n");
  int i; zposo temp;
 
  for (i = (quatnum / 2); i >= 0; i--) {
    siftDown(zposobj, i, quatnum - 1);
  }
 
  for (i = quatnum-1; i >= 1; i--) {
    // Swap
    temp = zposobj[0];
    zposobj[0] = zposobj[i];
    zposobj[i] = temp;
 
    siftDown(zposobj, 0, i-1);
  }
}