# include "relphot.h"

void sort_stars (radec, stars, Nstars)
int  **radec;
Star  *stars;
int    Nstars;
{
  
  int i;
  double *RAs;

  ALLOCATE (radec[0], int, Nstars);
  ALLOCATE (RAs, double, Nstars);
  for (i = 0; i < Nstars; i++) {
    radec[0][i] = i;
    RAs[i] = stars[i].RA;
  }

  sort_seq (radec[0], RAs, Nstars);
  free (RAs);

}


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


