void heapsort(int a[],int n) { int k,t; for (k = n/2 ; k >= 0; k--) downheap(a,n,k); while (n > 0) { t = a[0]; a[0] = a[n];a[n] = t; downheap(a, --n,0); } }
void heapsort (int *a, int n) { int i; for (i = (n - 2) / 2; i >= 0; i--) { downheap(a, n, i); } for (i = 0; i < n; i++) { int t = a[n - i - 1]; a[n - i - 1] = a[0]; a[0] = t; downheap(a, n - i - 1, 0); } }
/* Función de heapsort genérica. Esta función ordena mediante heap_sort * un arreglo de punteros opacos, para lo cual requiere que se * le pase una función de comparación. Modifica el arreglo "in-place". * Notar que esta función NO es formalmente parte del TAD Heap. */ void heap_sort(void *elementos[], size_t cant, cmp_func_t cmp){ if (cant <= 1) return; // heapify! size_t i = PADRE(cant - 1); // ultimo padre do { downheap(elementos, cant, i, cmp); } while (i--); // ordenar el arreglo de heap_max for (i = cant - 1; i > 0; i--) { swap(elementos, 0, i); // (nuevo) maximo al final downheap(elementos, i, 0, cmp); } }
void heapsort (int a[],int N) { int k,t; for (k = N/2;k >= 1; k--) downheap(a,N,k); while (N >1) { t = a[1]; a[1] = a[N]; a[N] = t; downheap(a,--N,1); } }
int gsl_heapsort_index (size_t * p, const void *data, size_t count, size_t size, gsl_comparison_fn_t compare) { /* Sort the array in ascending order. This is a true inplace algorithm with N log N operations. Worst case (an already sorted array) is something like 20% slower */ size_t i, k, N; if (count == 0) { return GSL_SUCCESS; /* No data to sort */ } for (i = 0; i < count; i++) { p[i] = i ; /* set permutation to identity */ } /* We have n_data elements, last element is at 'n_data-1', first at '0' Set N to the last element number. */ N = count - 1; k = N / 2; k++; /* Compensate the first use of 'k--' */ do { k--; downheap (p, data, size, N, k, compare); } while (k > 0); while (N > 0) { /* first swap the elements */ size_t tmp = p[0]; p[0] = p[N]; p[N] = tmp; /* then process the heap */ N--; downheap (p, data, size, N, 0, compare); } return GSL_SUCCESS; }
static void rebuild(CHEAP *heap) { int i; for (i = parent(GB.Count(heap->h) - 1); i >= 0; i--) downheap(heap, i); }
int pq_remove() { int value = heap[1]; heap[1] = heap[nheap--]; downheap(1); return value; }
void PriorityQueue::remove(PriorityItem* item) { if(item == 0x0) return; item->priority = 2<<31-1; downheap(top()); _remove(this,item); }
/* Restore heap property "from scratch" (without any prior assumptions). * Works in a bottom-up manner, which is more efficient than top-down */ static void heapify (airHeap *h) { unsigned int len = h->key_a->len; int maxi = len/2-1, i; /* above maxi, downheap has no effect */ for (i=maxi; i>=0; i--) { downheap(h, i); } }
/* Removes element ai from the heap, returns 0 upon success. */ int airHeapRemove(airHeap *h, unsigned int ai) { unsigned int old_invidx_ai; if (h==NULL || h->key_a->len<=ai) return 1; /* in the tree, replace ai with last element */ old_invidx_ai=h->invidx[ai]; h->idx[h->invidx[ai]]=h->idx[h->key_a->len-1]; h->invidx[h->idx[h->key_a->len-1]]=h->invidx[ai]; /* remove ai - copy last element over, then drop it */ if (ai!=h->key_a->len-1) { h->key[ai]=h->key[h->key_a->len-1]; if (h->data_a!=NULL) { memcpy((char*)h->data_a->data+ai*h->data_a->unit, (char*)h->data_a->data+(h->key_a->len-1)*h->data_a->unit, h->data_a->unit); } h->idx[h->invidx[h->key_a->len-1]]=ai; h->invidx[ai]=h->invidx[h->key_a->len-1]; } heapLenIncr(h, -1); /* push moved element downheap */ if (old_invidx_ai<h->key_a->len) downheap(h, old_invidx_ai); return 0; }
int abstractHeap::downheap(int k) { int j; void *t = heap[k]; int fk = (numels%2)?((numels-1)/2):(numels/2); if (k > fk) return k; j = k+k; if (j < numels && compare(heap[j], heap[j+1]) >= 0) j++; void *f = heap[j]; if (compare(t, f) >= 0) { heap[k] = f; heap[j] = t; if (positions != NULL) { positions[(j_voidint)f] = k; positions[(j_voidint)t] = j; } return downheap(j); } return k; }
/* move an element suitably so it is in a correct place */ static inline void adjustheap (ANHE *heap, int N, int k) { if (k > HEAP0 && heap[k].at <= heap[HPARENT(k)].at ) upheap (heap, k); else downheap (heap, N, k); }
void downheap(heap_t* heap, int i, int fin) { if (heap==NULL) return; if (heap_esta_vacio(heap)) return; int izq=2*i+1; int der=2*i+2; int maslargo=i; if (fin >= izq+1){ //verifica que tiene hijos if (fin == izq+1) //caso en el que tiene solo hijo izq { if (heap->cmp(heap->datos[i],heap->datos[izq])<0) swap(&heap->datos[i],&heap->datos[izq]); } else{ //tiene los 2 hijos if (heap->cmp(heap->datos[i],heap->datos[izq])<0) maslargo=izq; if (heap->cmp(heap->datos[maslargo],heap->datos[der])<0) maslargo=der; if (maslargo != i){ swap(&heap->datos[i],&heap->datos[maslargo]); downheap(heap,maslargo,fin); } } } }
void heapsort() { buildheap(); while (n > 1) { n--; swap(a[0], a[n]); downheap(0); } }
void Heap::delete_root() { std::swap(m_heap.at(0), m_heap.at(m_heap.size() - 1)); m_heap.pop_back(); if (m_heap.size() >= 1) downheap(0); }
/** * Liefert als Resultat das Heap-Element a[k], entfernt a[k] * aus dem Heap und stellt die Heap-Eigenschaft wieder her. */ static int removeh(int *a, int k) { int v = a[k]; a[k] = a[--N]; if ((k > 0) && (a[k] < a[k / 2])) upheap(a, k); else downheap(a, k); return v; }
/* Assigns a new key (and optionally, new data) to the front element and * re-sorts the heap if necessary. */ int airHeapFrontUpdate(airHeap *h, double newKey, const void *newData) { if (h==NULL || h->key_a->len==0) return 1; if (newData!=NULL && h->data_a!=NULL) memcpy((char*)h->data_a->data+h->idx[0]*h->data_a->unit, newData, h->data_a->unit); h->key[h->idx[0]]=newKey; downheap(h, 0); return 0; }
void Heap::heapify(std::vector<size_t>& array, size_t array_size) { if (array_size == 0) return; for (size_t i = array_size / 2; i < array_size; i++) downheap(static_cast<int>(i)); heapify(array, array_size / 2); }
MxHeapable *MxHeap::extract() { if( length() < 1 ) return NULL; swap(0, length()-1); MxHeapable *dead=drop(); downheap(0); dead->not_in_heap(); return dead; }
void Heap::downheap(int node) { if (SatisfiesHeapProperty(node)) return; int indexOfMinValueChild = GetIndexOfMinValueChild(node); std::swap(m_heap.at(node), m_heap.at(indexOfMinValueChild)); downheap(indexOfMinValueChild); }
void heapsort() { print(a, SIZE); buildheap(); print(a, SIZE); while (n > 1) { n--; exchange(0, n); downheap(0); } print(a, SIZE); }
void heapDownheap( void * v){ int i = heapIndex( v ); if( !i ){ fprintf(stderr, "There is no such element in heap \n"); return; } downheap(i); }
/** * Läßt a[k] im Feld versickern. */ static void downheap(int *a, int k) { int j = 2 * k; if (j < N) { // a[k] hat linken Sohn a[j] if (j + 1 < N) // a[k] hat auch rechten Sohn a[j+1] if (a[j] > a[j + 1]) j++; // Jetzt ist a[j] der kleinere Sohn von a[k] if (a[k] > a[j]) { exchange(a, k, j); downheap(a, j); } } }
/* Elimina el elemento con máxima prioridad, y lo devuelve. * Si el heap esta vacío, devuelve NULL. * Pre: el heap fue creado. * Post: el elemento desencolado ya no se encuentra en el heap. */ void *heap_desencolar(heap_t *heap){ if (heap_esta_vacio(heap)) return NULL; void* elem_max = heap->datos[0]; heap->datos[0] = heap->datos[--heap->cant]; downheap(heap->datos, heap->cant, 0, heap->heap_cmp); if (heap->tam / 2 > TAM_INICIAL && heap->cant < heap->tam * FACTOR_MIN) heap_redimensionar(heap, heap->tam / 2); return elem_max; }
void heapsort(struct cell **head, int n) { struct cell **p; int temp; int i; for(i = n/2; i > 0; i--) downheap(head, i, n); while(n > 1) { p = get_v(head, n); temp = (*head)->value; (*head)->value = (*p)->value; (*p)->value = temp; downheap(head, 1, n-1); n--; } }
void MxHeap::update(MxHeapable *t, float v) { SanityCheck( t->is_in_heap() ); t->heap_key(v); unsigned int i = t->get_heap_pos(); if( i>0 && v>ref(parent(i))->heap_key() ) upheap(i); else downheap(i); }
void gsl_heapsort (void *data, size_t count, size_t size, gsl_comparison_fn_t compare) { /* Sort the array in ascending order. This is a true inplace algorithm with N log N operations. Worst case (an already sorted array) is something like 20% slower */ size_t N; size_t k; if (count == 0) { return; /* No data to sort */ } /* We have n_data elements, last element is at 'n_data-1', first at '0' Set N to the last element number. */ N = count - 1; k = N / 2; k++; /* Compensate the first use of 'k--' */ do { k--; downheap (data, size, N, k, compare); } while (k > 0); while (N > 0) { /* first swap the elements */ swap (data, size, 0, N); /* then process the heap */ N--; downheap (data, size, N, 0, compare); } }
void *heap_desencolar(heap_t *heap){ if (heap==NULL) return NULL; if (heap_esta_vacio(heap)) return NULL; void* aux=heap->datos[0]; //cambio la raiz por la ultima hoja,la borro, y hago downheap con la nueva raiz. heap->datos[0]=heap->datos[heap->cantidad-1]; heap->datos[heap->cantidad-1]=NULL; heap->cantidad--; downheap(heap,0,heap->cantidad); return aux; }
void *abstractHeap::removeHead() { void *t = heap[1]; if (positions != NULL) positions[(j_voidint)t] = 0; heap[1] = heap[numels--]; if (numels) { if (positions != NULL) positions[(j_voidint)heap[1]] = 1; downheap(1); } return t; }
void* heap_remove(heap_t h) { if (h->count == 0) return NULL; void *res = h->tree[0]; h->tree[0] = h->tree[--h->next]; h->count--; downheap(h); return res; }