示例#1
0
void heapsort(PPD &ppd,PPI &ppi,VArrayL &ndx,ulong left,ulong right, BOOL b) {
// heapsort by prices
   long perc=0, oldperc=0; // percentage done

// creating a heap...
   for (long k=(right-left+1)/2+left;k>=(long)left; k--) { // left-1 ?!
      siftdown(ppd,ppi,ndx,left,right,k,b);
      perc = (1-(float)(k-left)/((right-left)/2))*50;
      if (perc>oldperc) {
         cout.width(3);
         cout << (oldperc=perc) << '%' << "\x8\x8\x8\x8";
      }
   }

// heap created.  Now sorting...
   for (k=right; k>left; ) {
      swap(ndx,left,k);
      siftdown(ppd,ppi,ndx,left,--k,left,b);
      perc=(2-(float)(k-left)/(right-left))*50;
      if (perc>oldperc) {
         cout.width(3);
         cout << (oldperc=perc) << '%' << "\x8\x8\x8\x8";
      }
   }

   cout << (right-left+1) << " elements done.\n";

}
示例#2
0
static void 
timerproc ( void ) 
{ 
int64 delta , now; 
Timer *t; 
void ( *f ) ( int64 , Eface ) ; 
Eface arg; 
#line 198 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
for ( ;; ) { 
runtime·lock ( &timers ) ; 
timers.sleeping = false; 
now = runtime·nanotime ( ) ; 
for ( ;; ) { 
if ( timers.len == 0 ) { 
delta = -1; 
break; 
} 
t = timers.t[0]; 
delta = t->when - now; 
if ( delta > 0 ) 
break; 
if ( t->period > 0 ) { 
#line 213 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
t->when += t->period * ( 1 + -delta/t->period ) ; 
siftdown ( 0 ) ; 
} else { 
#line 217 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
timers.t[0] = timers.t[--timers.len]; 
timers.t[0]->i = 0; 
siftdown ( 0 ) ; 
t->i = -1; 
} 
f = ( void* ) t->fv->fn; 
arg = t->arg; 
runtime·unlock ( &timers ) ; 
if ( raceenabled ) 
runtime·raceacquire ( t ) ; 
f ( now , arg ) ; 
#line 230 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
f = nil; 
USED ( f ) ; 
arg.type = nil; 
arg.data = nil; 
USED ( &arg ) ; 
#line 236 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
runtime·lock ( &timers ) ; 
} 
if ( delta < 0 ) { 
#line 240 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
timers.rescheduling = true; 
runtime·parkunlock ( &timers , "timer goroutine (idle)" ) ; 
continue; 
} 
#line 245 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
timers.sleeping = true; 
runtime·noteclear ( &timers.waitnote ) ; 
runtime·unlock ( &timers ) ; 
runtime·notetsleepg ( &timers.waitnote , delta ) ; 
} 
} 
示例#3
0
文件: heapsort.c 项目: baifanmvp/sort
void  heapsort (KEY_T array[], int len)
{
    register int	i;
    register KEY_T	temp, *sa = array - 1;
    /*
     |  'sa[]' is 'array[]' "shifted" one position to the left
     |  i.e.  sa[i] == array[i - 1]  (in particular:   sa[1] == array[0])
     |  'sa' has "Pascalish" indices and is thus more convenient
     |  for heap-sorting. An index i obeys the law:
     |	left_child (i) == 2i,  right_child (i) == 2i + 1
     |  Note: we don't use a[-1] which is outside our range, we just
     |  fake an array that starts one address lower, so we can refer
     |  to its first element as a[1] (rather than as a[0])
     */

    /* first step: make 'sa[]' a heap using siftdown len/2 times */
    for (i = len / 2; i >= 1; i--)
	siftdown (sa, i, len);

    /* heapify N more times, to reach complete order */
    for (i = len; i >= 2; i--) {
	SWAP(sa[1], sa[i]);
	siftdown (sa, 1, i - 1);
    }
}
示例#4
0
文件: sort.c 项目: wyc2012/wyc
void hsort(DType x[], int n)
{	int i;
	x--;//注意这里
	for (i = n/2; i >= 1; i--)
		siftdown(x, i, n);
	for (i = n; i >= 2; i--) {
		swap(x, 1, i);
		siftdown(x, 1, i-1);
	}
	x++;//注意这里
}
示例#5
0
void *cp_heap_pop(cp_heap *h)
{
    int rc;
    void *retval = NULL;

    if (h == NULL) return NULL;

    if ((rc = cp_heap_txlock(h))) return NULL;

    if (h->heap != NULL && h->heap_end > 1) 
    {
        retval = h->heap[1];
#ifdef _HEAP_TRACE
        DEBUGMSG("cp_heap_pop: %p\n", retval);
#endif
        if ((h->mode & COLLECTION_MODE_DEEP) && h->dtr)
            (*h->dtr)(retval);

        h->heap[1] = h->heap[h->heap_end - 1];
        h->heap_end--;
        siftdown(h, 1);
    }

    cp_heap_txunlock(h);

    return retval;
}
示例#6
0
bool 
runtime·deltimer ( Timer *t ) 
{ 
int32 i; 
#line 156 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
i = t->i; 
USED ( i ) ; 
#line 159 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
runtime·lock ( &timers ) ; 
#line 164 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
i = t->i; 
if ( i < 0 || i >= timers.len || timers.t[i] != t ) { 
runtime·unlock ( &timers ) ; 
return false; 
} 
#line 170 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
timers.len--; 
if ( i == timers.len ) { 
timers.t[i] = nil; 
} else { 
timers.t[i] = timers.t[timers.len]; 
timers.t[timers.len] = nil; 
timers.t[i]->i = i; 
siftup ( i ) ; 
siftdown ( i ) ; 
} 
if ( debug ) 
dumptimers ( "deltimer" ) ; 
runtime·unlock ( &timers ) ; 
return true; 
} 
示例#7
0
// Heapinsert inserts x into heap h according to h->less.
// It returns 1 on success, otherwise 0.
int
heapinsert(Heap *h, void *x)
{
    int k;

    if (h->len == h->cap) {
        void **ndata;
        int ncap = (h->len+1) * 2; /* allocate twice what we need */

        ndata = malloc(sizeof(void*) * ncap);
        if (!ndata) {
            return 0;
        }

        memcpy(ndata, h->data, sizeof(void*)*h->len);
        free(h->data);
        h->data = ndata;
        h->cap = ncap;
    }

    k = h->len;
    h->len++;
    set(h, k, x);
    siftdown(h, k);
    return 1;
}
示例#8
0
文件: dheap.cpp 项目: dadaism/GraphLB
void dheap::remove(item i) {
// Remove item i from heap. Name remove is used since delete is C++ keyword.
	int j = h[n--];
	     if (i != j && kvec[j] <= kvec[i]) siftup(j,pos[i]);
	else if (i != j && kvec[j] >  kvec[i]) siftdown(j,pos[i]);
	pos[i] = Null;
}
示例#9
0
static status siftdown (heap * const p_H,
                        size_t const index)
{
    size_t const left_child_index = first_child_index(index);
    if (left_child_index >= p_H -> next_element)
        return OK;

    {
        size_t right_child_index = left_child_index + 1;

        /*select the index of the child that is candidate for swapping */
        size_t cmp_target_index =
            right_child_index == p_H -> next_element ?
            left_child_index :
            (*(p_H -> p_cmp_f))(p_H -> base[left_child_index],
                                p_H -> base[right_child_index]) > 0?
            right_child_index :
            left_child_index;

        if ((*(p_H -> p_cmp_f))(p_H -> base[index],
                                p_H -> base[cmp_target_index]) > 0)
        {
            swap(p_H, index, cmp_target_index);
            return siftdown(p_H, cmp_target_index);
        }//if
    }//scope

    return OK;
}//siftdown
示例#10
0
文件: heap.c 项目: farsightsec/axa
static int
siftup(struct heap *h, size_t pos)
{
	assert(pos < ptrvec_size(h->vec));
	void *newitem = ptrvec_value(h->vec, pos);
	size_t endpos = ptrvec_size(h->vec);
	size_t startpos = pos;
	size_t childpos = 2 * pos + 1;
	while (childpos < endpos) {
		size_t rightpos = childpos + 1;
		if (rightpos < endpos) {
			int cmp = cmp_wrapper(h->cmp,
					      ptrvec_value(h->vec, childpos),
					      ptrvec_value(h->vec, rightpos));
			if (cmp == -1)
				return (-1);
			if (cmp == 0)
				childpos = rightpos;
		}
		ptrvec_data(h->vec)[pos] = ptrvec_value(h->vec, childpos);
		pos = childpos;
		childpos = 2 * pos + 1;
	}
	ptrvec_data(h->vec)[pos] = newitem;
	return (siftdown(h, startpos, pos));
}
示例#11
0
void test005()
{
    int arr[] = {2, 9, 1, 5, 4, 3};
    int i = 0;
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
    quickSort(arr, 6);
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
    siftdown(arr, 1, 6);
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
    siftup(arr , 2, 6);
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
}
示例#12
0
文件: bh.c 项目: braveyly/codefactory
/*
 * Function: void *bh_pop( Bh *h )
 * Description: delete the root element and return it
 * Input:  h: the bh object
 * Output: none
 * Return: void*: the root element
 * Others: none
 */
void *bh_pop( Bh *h )
{
	int		rc;
	void	*retval = NULL;

	if( h == NULL )
	{
		return NULL;
	}

	bh_info( h );

	if( h->heap != NULL && h->heap_end >= 1 )
	{
		retval = h->heap[1];

		if( h->dtr )
		{
			( *h->dtr )( retval );
		}

		h->heap[1] = h->heap[h->heap_end]; // root points to the last node
		h->heap_end--;
		siftdown( h, 1 );
	}

	return retval;
}
示例#13
0
void Heap::remove_top() {
    if (p == 0)
        return;
    root[0] = root[--p];
    if (p != 0)
        siftdown(0);
}
示例#14
0
void Heap::remove(int oldval)
{
  int i=p[oldval];
  a[i]=a[h--];
  p[a[i]]=i;
  siftdown(i);
  siftup(i);
}
示例#15
0
文件: Tree.c 项目: flow-J/Exercise
void creat()
{
    int i;
    for(i=n/2;i>=1;i--)
    {
        siftdown(i);
    }
}
static void flashpage_heapsort(void)
{
  short i;
  unsigned short** lines = lineindexstart;
  unsigned short count = lineindexend - lineindexstart;

  for (i = count / 2; i >= 0; i--)
  {
    siftdown(i, count - 1);
  }
  for (i = count - 1; i >= 0; i--)
  {
    unsigned short* temp = lines[0];
    lines[0] = lines[i];
    lines[i] = temp;
    siftdown(0, i - 1);
  }
}
示例#17
0
文件: Tree.c 项目: flow-J/Exercise
int deletemax()
{
    int t;
    t=h[1];
    h[1]=h[n];
    n--;
    siftdown(1);
    return t;
}
示例#18
0
void heapsort(void** arr, int size, int(*comp)(void*,void*)){
  mkheap(arr,size,comp);
  int end = size-1;
  while(end>0){
    swap(&arr[end],&arr[0]);
    end--;
    siftdown(arr,0,end,comp);
  }
}
示例#19
0
vertex* pq_remove(pqueue* pq)
{
  if( pq->n == 0 ) return NULL;
  
  pq->n--;
  swap(0, pq->n, pq);
  if (pq->n != 0)          /* Not on last element */
    siftdown(0, pq);       /* Put new heap root val in correct place */
  return pq->elems[pq->n];
}
示例#20
0
/*======================================================================\
* Author     (作者): i.sshe
* Date       (日期): 2016/03/27
* Others     (其他):
\*=======================================================================*/
void create_min_heap(int *heap_array, int len)
{
    int     i = len/2;      //最后一个页节点的父节点.

    for (; i > 0; i--)      //不能=0!
    {
        siftdown(heap_array, i, len);
    }

}
示例#21
0
文件: 4.3.c 项目: TonyChouZJU/pat-1
void heapify(Tree** heap, int n) {
    for(int i = (n - 2) / 2; i >= 0; i--) {
        siftdown(heap, n, i);
    }
//    printf("After Heapify: ");
//    for(int i = 0; i<n; i++) {
//        printf("%c ", heap[i]->key);
//    }
//    printf("\n");
}
示例#22
0
文件: 4.3.c 项目: TonyChouZJU/pat-1
Tree* heapDelete(Tree** heap, int n) {
    Tree* temp = heap[0];
    heap[0] = heap[n-1];
    heap[n-1] = temp;
    siftdown(heap, n-1, 0);
//        printf("After Delete: ");
//        for(int i = 0; i<n; i++) {
//            printf("%c ", heap[i]->key);
//        }
//        printf("\n");
    return heap[n-1];
}
int rmmin_heap(struct heap  *p)
{
  int min;
  //printf("sz before decrement:%d\n",p->sz);
  (p->sz)--;
  //printf("sz after decrement:%d\n",p->sz);
  min = p->item[0];
  p->item[0] = p->item[p->sz];
  p->item[p->sz] = min;
  //printf("item[0]= %d, Removed = %d\n", p->item[0], min);
  siftdown(p,0);
  //printf("sz at end of rmmin_heap:%d\n",p->sz);
  return(min); 
}
int get_k_largest_min_heap(int arr[], int size, int k){
    if(k<=0 || k>size) return INT_MIN;
    //min heap with size-k+1 elements
    int heap[k];
    int curr_idx = 0;
    for(int i=0; i<k; i++){
        heap[i] = arr[i];
        heap_insert(heap,i);
    }
    for(int i=k; i<size; i++){
        if(arr[i]<=heap[0]) continue;
        heap[0] = arr[i];
        siftdown(heap,k-1);
    }
    return heap[0];
}
示例#25
0
status heap_delete(heap * const p_H,
                   size_t const index,
                   generic_ptr * const p_data)
{
    if (index >= p_H -> next_element)
        return ERROR;

    *p_data = p_H -> base[index];
    p_H -> next_element --;

    p_H -> base[index] = p_H -> base [p_H -> next_element];

    return (siftup(p_H, index) == ERROR ||
            siftdown(p_H, index) == ERROR) ?
           ERROR :
           OK;
}//heap_delete
示例#26
0
static int
siftup(PyListObject *heap, Py_ssize_t pos)
{
    Py_ssize_t startpos, endpos, childpos, rightpos, limit;
    PyObject *tmp1, *tmp2;
    int cmp;

    assert(PyList_Check(heap));
    endpos = PyList_GET_SIZE(heap);
    startpos = pos;
    if (pos >= endpos) {
        PyErr_SetString(PyExc_IndexError, "index out of range");
        return -1;
    }

    /* Bubble up the smaller child until hitting a leaf. */
    limit = endpos / 2;          /* smallest pos that has no child */
    while (pos < limit) {
        /* Set childpos to index of smaller child.   */
        childpos = 2*pos + 1;    /* leftmost child position  */
        rightpos = childpos + 1;
        if (rightpos < endpos) {
            cmp = PyObject_RichCompareBool(
                      PyList_GET_ITEM(heap, childpos),
                      PyList_GET_ITEM(heap, rightpos),
                      Py_LT);
            if (cmp == -1)
                return -1;
            if (cmp == 0)
                childpos = rightpos;
            if (endpos != PyList_GET_SIZE(heap)) {
                PyErr_SetString(PyExc_RuntimeError,
                                "list changed size during iteration");
                return -1;
            }
        }
        /* Move the smaller child up. */
        tmp1 = PyList_GET_ITEM(heap, childpos);
        tmp2 = PyList_GET_ITEM(heap, pos);
        PyList_SET_ITEM(heap, childpos, tmp2);
        PyList_SET_ITEM(heap, pos, tmp1);
        pos = childpos;
    }
    /* Bubble it up to its final resting place (by sifting its parents down). */
    return siftdown(heap, startpos, pos);
}
示例#27
0
/*======================================================================\
* Author     (作者): i.sshe
* Date       (日期): 2016/03/27
* Others     (其他): 删除root节点
*                    输出根->把最后一个放到根->堆减1->调整, 一直循环.
\*=======================================================================*/
 void delete_root_and_sort(int *heap_array, int len)
{
     int temp_len = len;
     int temp = 0;
     int i = 0;

     for (i = 1; i < len + 1; i++)
     {
        temp = heap_array[1];
        heap_array[1] = heap_array[temp_len];
        temp_len--;
        siftdown(heap_array, 1, temp_len);
        printf("%d ", temp);
     }
     printf("\n");

}
示例#28
0
void *
heapremove(Heap *h, int k)
{
    void *x;

    if (k >= h->len) {
        return 0;
    }

    x = h->data[k];
    h->len--;
    set(h, k, h->data[h->len]);
    siftdown(h, k);
    siftup(h, k);
    h->rec(x, -1);
    return x;
}
示例#29
0
extern int heapextract( table_t *t, entry_t *item )
{
	entry_t *p;

	if (t->size == 0 ) {
		return 0;
	}

	p = &(t->heap[0]);

	memcpy( item, p, sizeof( entry_t ));
	memcpy( &(t->heap[0]), &(t->heap[t->size-1]), sizeof(entry_t) );

	siftdown(t,t->size, 0);
	t->size--;

	return 1;
}
示例#30
0
static PyObject *
heappush(PyObject *self, PyObject *args)
{
    PyObject *heap, *item;

    if (!PyArg_UnpackTuple(args, "heappush", 2, 2, &heap, &item))
        return NULL;

    if (!PyList_Check(heap)) {
        PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
        return NULL;
    }

    if (PyList_Append(heap, item) == -1)
        return NULL;

    if (siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1) == -1)
        return NULL;
    Py_RETURN_NONE;
}