void heap_sort(int array[], int size, int top) { int i = 0; for(i = size/2-1; i >= 0; i--) heap_adjust(array, i, size); for(i = size-1; i > size-top; i--) { swap(&array[0], &array[i]); heap_adjust(array, 0, i); } }
void K_min(int arr[],const int count,const int K){ int i,min; /*建立小顶堆*/ for(i = count/2 - 1;i>=0;i--) heap_adjust(arr,i,count); /*一次筛选出最小的元素*/ for(i=0;i<K;i++){ min = arr[0]; printf("%4d",min); arr[0] = arr[count - i -1]; heap_adjust(arr,0,count -i -1); } printf("\n"); }
void top_collect(knn_item_t * knns, int k, long long idx, real similarity) { if(similarity <= knns[0].similarity) return; knns[0].similarity = similarity; knns[0].idx = idx; heap_adjust(knns, 0, k); }
int heap_sort(int array[], int length) { for (int i = length / 2 - 1; i >= 0; i--) { heap_adjust(array, length, i); } printf("Adjusted Heap:\n"); print_array(array, length); printf("Adjusted Heap:\n"); for (int i = length - 1; i > 0; i--) { swap(array, 0, i); heap_adjust(array, i, 0); } return 0; }
void heap_sort(int *a, const int n) { heap_init(a, n); int i; for(i = n - 1; i >= 0; i--) { swap(a + i, a); heap_adjust(a, 0, i); } } //堆排序
void heap_sort(int *a, int n) { int i; for (i=n/2-1; i>=0; i--) { heap_adjust(a, n, i); } my_print(a, n); for (i=n-1; i>0; i--) { a[i] = a[i]^a[0]; a[0] = a[i]^a[0]; a[i] = a[i]^a[0]; heap_adjust(a, i, 0); my_print(a, n); } }
void heap_sort(Sqlist *L) { int k; for (k=L->length/2; k>=1; k--) { heap_adjust(L, k, L->length); } for (k=L->length; k>1; k--) { L->R[0].key = L->R[k].key; L->R[k].key = L->R[1].key; L->R[1].key = L->R[0].key; heap_adjust(L, 1, k-1); } }
void heap_sort(vector<int> &array, int size) { int i; build_heap(array, size); for (i = size; i >= 1; i--) { swap(array[1], array[i]); heap_adjust(array, 1, i - 1); print_sort(array); } }
static void __timer_delete(struct timer *timer) { size_t i = timer->i; if (i != --timer_context.n) { timer_context.heap[i] = timer_context.heap[timer_context.n]; timer_context.heap[i]->i = i; heap_adjust(i); } timer->i = TIMER_INVALID_INDEX; }
/* * Add a timer to the heap. Do this by adding it at the end, then * moving it up or down as necessary to achieve partial ordering. */ static void heap_insert(TimerHeap *heap, Timer *timer) { heap->len++; if (heap->len > heap->size) { heap->tab = gw_realloc(heap->tab, heap->len * sizeof(heap->tab[0])); heap->size = heap->len; } heap->tab[heap->len - 1] = timer; timer->index = heap->len - 1; heap_adjust(heap, timer->index); }
void heap_sort(int *a, int length) { // 建立堆 大根堆,递增排序 heap_build(a,length); for (int i = length-1; i >0; --i) { //交换 heap_swop(&a[0],&a[i]); //调整 heap_adjust(a,i); } }
void top_sort(knn_item_t * knns, int k) { real similarity; long long idx; for(int i = k - 1; i > 0; i--) { similarity = knns[0].similarity; idx = knns[0].idx; knns[0].similarity = knns[i].similarity; knns[0].idx = knns[i].idx; knns[i].similarity = similarity; knns[i].idx = idx; heap_adjust(knns, 0, i); } }
/* * Remove a timer from the heap. Do this by swapping it with the element * in the last position, then shortening the heap, then moving the * swapped element up or down to maintain the partial ordering. */ static void heap_delete(TimerHeap *heap, long index) { long last; gw_assert(index >= 0); gw_assert(index < heap->len); gw_assert(heap->tab[index]->index == index); last = heap->len - 1; heap_swap(heap, index, last); heap->tab[last]->index = -1; heap->len--; if (index != last) heap_adjust(heap, index); }
void heap_adjust(int *a, const int i, const int n) { int l = 2 * i + 1; int r = 2 * i + 2; int largest; if(l < n && a[i] < a[l]) { largest = l; } else { largest = i; } if(r < n && a[largest] < a[r]) { largest = r; } if(i != largest) { swap(a + i, a + largest); heap_adjust(a, largest, n); } } //调整堆
void gw_timer_elapsed_start(Timer *timer, int interval, void *data) { int wakeup = 0; gw_assert(timer != NULL); if (timer == NULL) return; lock(timer->timerset); /* Convert to absolute time */ interval += time(NULL); if (timer->elapses > 0) { /* Resetting an existing timer. Move it to its new * position in the heap. */ if (interval < timer->elapses && timer->index == 0) wakeup = 1; timer->elapses = interval; gw_assert(timer->index >= 0); gw_assert(timer->timerset->heap->tab[timer->index] == timer); wakeup |= heap_adjust(timer->timerset->heap, timer->index); } else { /* Setting a new timer, or resetting an elapsed one. * There should be no further elapse event on the * output list here. */ /* abort_elapsed(timer); */ timer->elapsed_data = NULL; /* Then activate the timer. */ timer->elapses = interval; gw_assert(timer->index < 0); heap_insert(timer->timerset->heap, timer); wakeup = timer->index == 0; /* Do we have a new top? */ } if (data != NULL) { timer->data = data; } unlock(timer->timerset); if (wakeup) gwthread_wakeup(timer->timerset->thread); }
static void heap_adjust(int *a, int n, int p) { int child, temp; if (p > n/2-1) { return; } child = 2*p+1; if (child+1<n && a[child]<a[child+1]) child++; if (a[p] < a[child]) { temp = a[p]; a[p] = a[child]; a[child] = temp; } heap_adjust(a, n, child); }
void heap_adjust(vector<int> &array, int i, int size) { int lchild = 2 * i; int rchild = 2 * i + 1; int max = i; if (i <= size / 2) { if (lchild <= size && array[lchild] > array[max]) { max = lchild; } if (rchild <= size && array[rchild] > array[max]) { max = rchild; } if (max != i) { swap(array[i], array[max]); heap_adjust(array, max, size); } } }
void gwtimer_start(Timer *timer, int interval, WAPEvent *event) { int wakeup = 0; gw_assert(initialized); gw_assert(timer != NULL); gw_assert(event != NULL || timer->event != NULL); lock(timers); /* Convert to absolute time */ interval += time(NULL); if (timer->elapses > 0) { /* Resetting an existing timer. Move it to its new * position in the heap. */ if (interval < timer->elapses && timer->index == 0) wakeup = 1; timer->elapses = interval; gw_assert(timers->heap->tab[timer->index] == timer); wakeup |= heap_adjust(timers->heap, timer->index); } else { /* Setting a new timer, or resetting an elapsed one. * First deal with a possible elapse event that may * still be on the output list. */ abort_elapsed(timer); /* Then activate the timer. */ timer->elapses = interval; gw_assert(timer->index < 0); heap_insert(timers->heap, timer); wakeup = timer->index == 0; /* Do we have a new top? */ } if (event != NULL) { wap_event_destroy(timer->event); timer->event = event; } unlock(timers); if (wakeup) gwthread_wakeup(timers->thread); }
// Huffman compression/decompression function void compdecomp(byte * data, size_t data_len) { size_t i, j, n, mask; bits32 k, t; byte c; byte * cptr; byte * dptr = data; /* COMPRESSION */ // allocate data space byte * comp = (byte *)malloc(data_len + 1); size_t freq[512]; // allocate frequency table size_t heap[256]; // allocate heap int link[512]; // allocate link array bits32 code[256]; // huffman codes byte clen[256]; // bit lengths of codes memset(comp,0,sizeof(byte) * (data_len + 1)); memset(freq,0,sizeof(size_t) * 512); memset(heap,0,sizeof(size_t) * 256); memset(link,0,sizeof(int) * 512); memset(code,0,sizeof(bits32) * 256); memset(clen,0,sizeof(byte) * 256); // count frequencies for (i = 0; i < data_len; ++i) { ++freq[(size_t)(*dptr)]; ++dptr; } // create indirect heap based on frequencies n = 0; for (i = 0; i < 256; ++i) { if (freq[i]) { heap[n] = i; ++n; } } for (i = n; i > 0; --i) heap_adjust(freq,heap,n,i); // generate a trie from heap size_t temp; // at this point, n contains the number of characters // that occur in the data array while (n > 1) { // take first item from top of heap --n; temp = heap[0]; heap[0] = heap[n]; // adjust the heap to maintain properties heap_adjust(freq,heap,n,1); // in upper half of freq array, store sums of // the two smallest frequencies from the heap freq[256 + n] = freq[heap[0]] + freq[temp]; link[temp] = 256 + n; // parent link[heap[0]] = -256 - n; // left child heap[0] = 256 + n; // right child // adjust the heap again heap_adjust(freq,heap,n,1); } link[256 + n] = 0; // generate codes size_t m, x, maxx = 0, maxi = 0; int l; for (m = 0; m < 256; ++m) { if (!freq[m]) // character does not occur { code[m] = 0; clen[m] = 0; } else { i = 0; // length of current code j = 1; // bit being set in code x = 0; // code being built l = link[m]; // link in trie while (l) // while not at end of trie { if (l < 0) // left link (negative) { x += j; // insert 1 into code l = -l; // reverse sign } l = link[l]; // move to next link j <<= 1; // next bit to be set ++i; // increment code length } code[m] = (unsigned long)x; // save code clen[m] = (unsigned char)i; // save code len // keep track of biggest key if (x > maxx) maxx = x; // keep track of longest key if (i > maxi) maxi = i; } } // make sure longest codes fit in unsigned long-bits if (maxi > (sizeof(unsigned long) * 8)) { fprintf(stderr,"error: bit code overflow\n"); exit(1); } // encode data size_t comp_len = 0; // number of data_len output char bout = 0; // byte of encoded data int bit = -1; // count of bits stored in bout dptr = data; // watch for one-value file! if (maxx == 0) { fprintf(stderr,"error: file has only one value!\n"); exit(1); } for (j = 0; j < data_len; ++j) { // start copying at first bit of code mask = 1 << (clen[(*dptr)] - 1); // copy code bits for (i = 0; i < clen[(*dptr)]; ++i) { if (bit == 7) { // store full output byte comp[comp_len] = bout; ++comp_len; // check for output longer than input! if (comp_len == data_len) { fprintf(stderr,"error: no compression\n"); exit(1); } bit = 0; bout = 0; } else { // move to next bit ++bit; bout <<= 1; } if (code[(*dptr)] & mask) bout |= 1; mask >>= 1; } ++dptr; } // output any incomplete data_len and bits bout <<= (7 - bit); comp[comp_len] = bout; ++comp_len; // printf("data len = %u\n",data_len); // printf("comp len = %u\n",comp_len); /* DECOMPRESSION */ // allocate heap2 bits32 heap2[256]; // allocate output character buffer char outc[256]; // initialize work areas memset(heap2,0,256 * sizeof(bits32)); // create decode table as trie heap2 char * optr = outc; for (j = 0; j < 256; ++j) { (*optr) = (char)j; ++optr; // if code exists for this byte if (code[j] | clen[j]) { // begin at first code bit k = 0; mask = 1 << (clen[j] - 1); // find proper node, using bits in // code as path. for (i = 0; i < clen[j]; ++i) { k = k * 2 + 1; // right link if (code[j] & mask) ++k; // go left mask >>= 1; // next bit } heap2[j] = k; // store link in heap2 } }
void heap_init(int *a, const int n) { int i; for(i = n / 2; i >= 0; i--) { heap_adjust(a, i, n); } } //初始堆
void heap_min_sort(int* input, int input_size) { if(0 == input) return; for(int i=0; i<input_size; i+=1) { heap_adjust(input + i, input_size - i); } }
void top_init(knn_item_t * knns, int k) { for(int i = k / 2 - 1; i >= 0; i--) { heap_adjust(knns, i, k); } }
void build_heap(vector<int> &array, int size) { int i; for (i = size / 2; i >= 1; i--) heap_adjust(array, i, size); }