# include "uniphot.h"

void sort (unsigned int *X, int N) {

  int l,j,ir,i;
  unsigned tX;
  
  if (N < 2) return;

  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      l--;
      tX = X[l];
    }
    else {
      tX = X[ir];
      X[ir] = X[0];
      if (--ir == 0) {
	X[0] = tX;
	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];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    X[i] = tX;
  }
}

void sortB (double *X, double *Y, int N) {

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

  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      l--;
      tX = X[l];
      tY = Y[l];
    }
    else {
      tX = X[ir];
      X[ir] = X[0];
      tY = Y[ir];
      Y[ir] = Y[0];
      if (--ir == 0) {
	X[0] = tX;
	Y[0] = tY;
	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];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    X[i] = tX;
    Y[i] = tY;
  }
}

void sortD (double *X, double *Y, double *Z, int N) {

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

  l = N >> 1;
  ir = N - 1;
  for (;;) {
    if (l > 0) {
      l--;
      tX = X[l];
      tY = Y[l];
      tZ = Z[l];
    }
    else {
      tX = X[ir];
      X[ir] = X[0];
      tY = Y[ir];
      Y[ir] = Y[0];
      tZ = Z[ir];
      Z[ir] = Z[0];
      if (--ir == 0) {
	X[0] = tX;
	Y[0] = tY;
	Z[0] = tZ;
	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];
	Z[i] = Z[j];
	j += (i=j) + 1;
      }
      else j = ir + 1;
    }
    X[i] = tX;
    Y[i] = tY;
    Z[i] = tZ;
  }
}

