
sort (float *value, int N) {

  int l,j,ir,i;
  float temp;
  
  if (N < 2) return;
  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      temp = value[--l];
    }
    else {
      temp = value[ir];
      value[ir] = value[0];
      if (--ir == 0) {
	value[0] = temp;
	return;
      }
    }
    i = l;
    j = (l << 1) + 1;
    while (j <= ir) {
      if (j < ir && value[j] < value[j+1]) ++j;
      if (temp < value[j]) {
	value[i]=value[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    value[i] = temp;
  }
}

sortkey (int *index, float *key, int N) {

  int l,j,ir,i;
  float ktmp;
  int  itmp;
  
  if (N < 2) return;
  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      l--;
      ktmp = key[l];
      itmp = index[l];
    }
    else {
      ktmp    = key[ir];
      key[ir] = key[0];
      itmp      = index[ir];
      index[ir] = index[0];
      ir--;
      if (ir == 0) {
	key[0] = ktmp;
	index[0] = itmp;
	return;
      }
    }
    i = l;
    j = (l << 1) + 1;
    while (j <= ir) {
      if (j < ir && key[j] < key[j+1]) ++j;
      if (ktmp < key[j]) {
	key[i] = key[j];
	index[i] = index[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    key[i] = ktmp;
    index[i] = itmp;
  }
}
