# include "gastro2.h"

void sort_lum (double *R, double *X, double *Y, int N) {

  int l,j,ir,i;
  double tX, tY, tR;
  
  if (N < 2) return;

  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      l--;
      tX = X[l];
      tY = Y[l];
      tR = R[l];
    }
    else {
      tX    = X[ir];
      X[ir] = X[0];
      tY    = Y[ir];
      Y[ir] = Y[0];
      tR    = R[ir];
      R[ir] = R[0];
      if (--ir == 0) {
	X[0] = tX;
	Y[0] = tY;
	R[0] = tR;
	return;
      }
    }
    i = l;
    j = (l << 1) + 1;
    while (j <= ir) {
      if (j < ir && R[j] < R[j+1]) j++;
      if (tR < R[j]) {
	X[i] = X[j];
	Y[i] = Y[j];
	R[i] = R[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    X[i] = tX;
    Y[i] = tY;
    R[i] = tR;
  }
}

void sort_lists (double *X, double *Y, int *S, int N) {

  int l,j,ir,i;
  double tX, tY, tS;
  
  if (N < 2) return;

  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      l--;
      tX = X[l];
      tY = Y[l];
      tS = S[l];
    }
    else {
      tX = X[ir];
      X[ir] = X[0];
      tY = Y[ir];
      Y[ir] = Y[0];
      tS = S[ir];
      S[ir] = S[0];
      if (--ir == 0) {
	X[0] = tX;
	Y[0] = tY;
	S[0] = tS;
	return;
      }
    }
    i = l;
    j = (l << 1) + 1;
    while (j <= ir) {
      if (j < ir && X[j] < X[j+1]) j++;
      if (tX < X[j]) {
	X[i] = X[j];
	Y[i] = Y[j];
	S[i] = S[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    X[i] = tX;
    Y[i] = tY;
    S[i] = tS;
  }
}

void sort (double *X, int N) {

  int l,j,ir,i;
  double tmp;

  if (N < 2) return;
  
  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      l--;
      tmp = X[l];
    }
    else {
      tmp = X[ir];
      X[ir] = X[0];
      if (--ir == 0) {
	X[0] = tmp;
	return;
      }
    }
    i = l;
    j = (l << 1) + 1;
    while (j <= ir) {
      if (j < ir && X[j] < X[j+1]) j++;
      if (tmp < X[j]) {
	X[i] = X[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    X[i] = tmp;
  }
}

void sort_stars_mag (StarData *stars, int N) {

  int l,j,ir,i;
  StarData tempstar;
  
  if (N < 2) return;

  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      tempstar = stars[--l];
    }
    else {
      tempstar = stars[ir];
      stars[ir] = stars[0];
      if (--ir == 0) {
	stars[0] = tempstar;
	return;
      }
    }
    i = l;
    j = (l << 1) + 1;
    while (j <= ir) {
      if (j < ir && stars[j].M < stars[j+1].M) ++j;
      if (tempstar.M < stars[j].M) {
	stars[i] = stars[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    stars[i] = tempstar;
  }
}

void sort_stars_X (StarData *stars, int N) {

  int l,j,ir,i;
  StarData tempstar;
  
  if (N < 2) return;

  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      tempstar = stars[--l];
    }
    else {
      tempstar = stars[ir];
      stars[ir] = stars[0];
      if (--ir == 0) {
	stars[0] = tempstar;
	return;
      }
    }
    i = l;
    j = (l << 1) + 1;
    while (j <= ir) {
      if (j < ir && stars[j].X < stars[j+1].X) ++j;
      if (tempstar.X < stars[j].X) {
	stars[i] = stars[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    stars[i] = tempstar;
  }
}


