Пример #1
0
/* Changes the value of an element in the heap and restores the
   heap property. This can take O(log(n)) time */
int Zoltan_Heap_Change_Value (HEAP *h, int element, float value)
{
int position, father;

  if (element < 0 || element >= h->space)
     return ZOLTAN_FATAL;                           /* Error */

  if ((position = h->pos[element]) >= 0) {
     if (value < h->value[element]) {
        h->value[element] = value;
        heapify(h,position);
        }
     else if (value > h->value[element]) {
        h->value[element] = value;
        father = (position-1)/2;
        while (position > 0 && h->value[element] > h->value[h->ele[father]]) {
           h->pos[h->ele[position]] = father;
           h->pos[h->ele[father]]   = position;
           INT_SWAP(h->ele[father], h->ele[position]);
           position = father;
           father   = (father-1)/2;
           }
        }
     }
  return ZOLTAN_OK;
}
void perm_subs(subject **s,int nsub)
{
int i,k;
for (i=nsub-1;i>=1;--i)
  {
  k=rand()%(i+1);
  INT_SWAP(s[i]->cc,s[k]->cc);
  }
}
Пример #3
0
// 
//   The following two functions are for the quicksort algorithm 
//
int partition(int *a, int p, int r, double *reference, int offset, int D){
  int i,j;
  double x,tmp;
  
  x= EVAL_INDEX(a[p],offset,D); i=p-1; j=r+1;
  while (1) {
    for (j--; EVAL_INDEX(a[j],offset,D) > x; j--);
    for (i++; EVAL_INDEX(a[i],offset,D) <x; i++);
    if (i<j){
      INT_SWAP(a[j],a[i]);
    }
    else return j;
  }
}
int alls_to_genotype(int *all,int n_alls)
{
int geno;
if (all[0]>all[1]) INT_SWAP(all[0],all[1]);
/* I know this is inefficient but I do not mind */
if (all[0]<=0 && all[1]<=0)
  geno=n_alls*n_alls+1;
else if (all[1]>0 && all[0]<=0)
  { error("Locus with only one allele missing",""); return 0; }
else if (all[1]>n_alls)
  { error("Locus with allele number greater than number of alleles in ",""); return 0; }
else 
  geno=(all[0]-1)*n_alls+all[1];
return geno-1;
}
Пример #5
0
/* Subroutine which gets the heap property if both subtrees already
   have the heap property. */
static void heapify (HEAP *h, int root)
{
int left = root*2 + 1, right = root*2 + 2, largest = root;

  if ((left < h->n)  && (h->value[h->ele[left ]] > h->value[h->ele[largest]]))
     largest = left;
  if ((right < h->n) && (h->value[h->ele[right]] > h->value[h->ele[largest]]))
     largest = right;
  if (largest != root) {
     h->pos[h->ele[root]]    = largest;
     h->pos[h->ele[largest]] = root;
     INT_SWAP(h->ele[root], h->ele[largest]);
     heapify(h, largest);
     }
}
Пример #6
0
int getMedian(int* arr, uint n)
{
	int low, high;
	int median;
	int middle, ll, hh;

	low = 0; high = n - 1; median = (low + high) >> 1;
	for (;;) {

		if (high <= low) // One element only
			return arr[median];

		if (high == low + 1) {  // Two elements only
			if (arr[low] > arr[high])
				INT_SWAP(arr[low], arr[high]);
			return arr[median];
		}

		/* Find median of low, middle and high items; swap into position low */
		middle = (low + high) >> 1;
		if (arr[middle] > arr[high])    INT_SWAP(arr[middle], arr[high]);
		if (arr[low] > arr[high])       INT_SWAP(arr[low], arr[high]);
		if (arr[middle] > arr[low])     INT_SWAP(arr[middle], arr[low]);

		/* Swap low item (now in position middle) into position (low+1) */
		INT_SWAP(arr[middle], arr[low + 1]);

		/* Nibble from each end towards middle, swapping items when stuck */
		ll = low + 1;
		hh = high;
		for (;;) {
			do ll++; while (arr[low] > arr[ll]);
			do hh--; while (arr[hh] > arr[low]);

			if (hh < ll)
				break;

			INT_SWAP(arr[ll], arr[hh]);
		}

		/* Swap middle item (in position low) back into correct position */
		INT_SWAP(arr[low], arr[hh]);

		/* Re-set active partition */
		if (hh <= median)
			low = ll;
		if (hh >= median)
			high = hh - 1;
	}
}