Beispiel #1
0
// variant
static void quick_sort2(int32_t *a, int32_t low, int32_t high) {
  int32_t i, j, p, aux;

  if (high <= low + 1) return;

  p = a[low];
  i = low;
  j = high;

  do { j--; } while (a[j] > p);
  do { i++; } while (i <= j && a[i] < p);

  while (i < j) {
    aux = a[i]; a[i] = a[j]; a[j] = aux;

    do { j--; } while (a[j] > p);
    do { i++; } while (a[i] < p);
  }

  a[low] = a[j];
  a[j] = p;

  quick_sort2(a, low, j);
  quick_sort2(a, j+1, high);
}
Beispiel #2
0
// Gyorsrendezés
void quick_sort2(int a[], int l, int r) {
    int j;
    if(l < r) {
	j = partition(a, l, r);
	quick_sort2(a, l, j - 1);
	quick_sort2(a, j + 1, r);
    }
}
Beispiel #3
0
void quick_sort2(int a[], int start, int end)
{
	if (start < end)
	{
		int pivot = partition(a, start, end);
		quick_sort2(a, start, pivot-1);
		quick_sort2(a, pivot+1, end);
	}
}
// Hoare edition QuickSort
void quick_sort2(int a[], int left, int right) {
	int i = left, j = right;
	// here can choose the median of a[left], a[mid], and a[right]
	int pivot = a[(left + right) / 2];
	while (i < j) {
		while (a[i] < pivot)
			i++;
		while (a[j] > pivot)
			j--;
		if (i <= j)
			swap(a[i++], a[j--]);
	}
	if (left < j)
		quick_sort2(a, left, j);
	if (i < right)
		quick_sort2(a, i, right);
}
Beispiel #5
0
void quick_sort2(int array[], int low, int high)
{
    while (low < high)
    {
	int pivot = partition(array, low, high);
	quick_sort2(array, low, pivot-1);
	low = pivot + 1;
    }
}
Beispiel #6
0
void quick_sort2(int *array, int length)
{
    int i = -1;
    int j = 0;
    int value = 0;
    if(length > 1){
        value = array[length - 1];
        for(j = 0; j < length - 1; ++j){
            if(array[j] < value){
                i++;
                swap(array + j, array + i, sizeof(array[j]));
            }
        }
        swap(array + i + 1, array + length - 1, sizeof(int));
        quick_sort2(array, i + 1);
        quick_sort2(array + i + 2, length - i - 2);
    }
}
Beispiel #7
0
void quick_sort2(int a[], int left, int right) {
    if (left >= right)
        return;
    int pos = left + (right - left) / 2;
    swap(&a[pos], &a[right]);
    int small = left - 1;
    for (int i=left; i<=right; i++) {
        if (a[i] < a[right]) {
            small++;
            if (small != i)
                swap(&a[small], &a[i]);
        }
    }
    pos = small + 0;
    swap(&a[pos], &a[right]);
    quick_sort2(a, left, pos-1);
    quick_sort2(a, pos+1, right);
    return;
}
Beispiel #8
0
void sort_radius(int N_cm, struct star *s, double *r2_sort, int *idx)
{
	int i;
	for (i=0;i<N_cm;++i) {
		r2_sort[i] = (s[i].x[0]*s[i].x[0]) + 
					 (s[i].x[1]*s[i].x[1]) + 
					 (s[i].x[2]*s[i].x[2]);
		idx[i] = i;
	}
	quick_sort2(r2_sort,idx,N_cm);
	return;
}
Beispiel #9
0
/* main */ 
int main()
{
	int a[] = {5, 4, 7, 3, 2, 9, 1, 8};
	int len = sizeof(a)/sizeof(a[0]);
	printf("sort befaore: \n");
	print(a, len);

	quick_sort2(a, 0, len-1);
	printf("sort after: \n");
	print(a, len);

	return 0;
}
Beispiel #10
0
int main(int argc, char **argv)
{
    int a[NUMBER];
    srand(time(NULL));
    int i;
    for (i=0; i<NUMBER; i++) {
        a[i] = rand() % (RANDMAX - RANDMIN) + RANDMIN;
        printf("%d ", a[i]);
    }
    printf("\n");

    quick_sort2(a, 0, NUMBER - 1);

    for (i=0; i<NUMBER; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}
Beispiel #11
0
int main(int argc, char **argv)
{
    int n = 0;
    int i = 0;
    int *array = NULL;
    n = atoi(argv[1]);
    array = (int *)malloc(sizeof(int) * n);
    for(i = 0; i < n; ++i){
        array[i] = rand();
    }
    //select_sort(array, n); 
    //insert_sort(array, n);
    //bubble_sort(array, n);
    //merge_sort(array, n);
    //quick_sort1(array, n);
    quick_sort2(array, n);
    print_array(array, n);
    return 0;
}
Beispiel #12
0
// Gyorsrendezés
void quick_sort(int a[], int n) {
    quick_sort2(a, 0, n - 1);
}
Beispiel #13
0
static void sort2(int32_t *a, int32_t n) {
  quick_sort2(a, 0, n);
}
Beispiel #14
0
/*
*  Here is the second sort function.
*/
void
quick_sort2(
    double *A,
    int *index,
    int l,
    int r
)
/*=== Sortiere A[l]...A[r] aufsteigend ===*/
{
    int i,j,t;
    double v;
    double td;

    if (r<=l) return;

    if (r==l+1) {
        if (A[l]>A[r]) {
            td = A[l];
            A[l] = A[r];
            A[r] = td;
            t = index[l];
            index[l] = index[r];
            index[r] = t;
        }
    }

    i = l-1;
    j = r;
    v = A[r];

    do {
        do i++;
        while(A[i]<=v && i<r);

        do j--;
        while (A[j]>v && j>=l);
        if (j==l && A[j]>v) j = l-1;

        if (j>i) {
            td = A[i];
            A[i] = A[j];
            A[j] = td;
            t = index[i];
            index[i] = index[j];
            index[j] = t;
        }
        else {
            td = A[i];
            A[i] = A[r];
            A[r] = td;
            t = index[i];
            index[i] = index[r];
            index[r] = t;
        }

    } while (j>i);

    quick_sort2(A,index,l,i-1);
    quick_sort2(A,index,i+1,r);

}/*quick_sort2*/
Beispiel #15
0
int alps_phase1prim(
					lpstruct *lp,
					int pril
					)
{
	/** Declare alps_phase1prim scalars */
	int i,j,k,jj;
	int row_ind_of_slack_art;
	int nextbasisheader;
	int randseed;
	int basnonz,ind,l,starttime,status,totalfill,totalcancel,stoptime;
	int freecount,freepos;
	int oneboundcount,oneboundpos;
	int twoboundscount,twoboundspos;
	double maxabsobjcoeff,absobjcoeff;
	double colmax,colrmax,colimax;
	int colrind,coliind;
	double faij;
	double objcorr;
	int initbasisstructs,initbasisslacks,initbasisartifs;

	/** Declare alps\_phase1prim arrays */
	int *rowcovered;
	int *rowfill;
	double *rowpivot;
	int *pivrow;
	int *pivcol;
	double *pivvalue;
	double *xbas;
	double *trhs;
	double *sortkey;
	int *sortindex;

	/** Allocate space for alps\_phase1prim arrays */
	rowcovered = (int *) malloc( lp->im * sizeof(int) );
	rowfill = (int *) malloc( lp->im * sizeof(int) );
	rowpivot = (double *) malloc( lp->im * sizeof(double) );
	sortkey = (double *) malloc( lp->inorig * sizeof(double) );
	sortindex = (int *) malloc( lp->inorig * sizeof(int) );
	pivrow = NULL;
	pivcol = NULL;
	pivvalue = NULL;
	xbas = (double *) malloc( lp->im * sizeof(double) );
	trhs = (double *) malloc( lp->im * sizeof(double) );
	if (!rowcovered || !rowfill || !rowpivot
		|| !sortkey || !sortindex || !xbas || !trhs )
		// run out of memory
	{
		fprintf (stderr, "run out of memory\n");
		return(ALPS_RUNOUTOFMEMORY);
	}

	/** Allocate space for solution and basis */
	lp->x = (double *) malloc( lp->in * sizeof(double) );
	lp->redcost = (double *) malloc( lp->in * sizeof(double) );
	lp->y = (double *) malloc( lp->im * sizeof(double) );
	lp->ysval = (double *) malloc( lp->im * sizeof(double) );
	lp->ysind = (int *) malloc( lp->im * sizeof(int) );
	lp->d = (double *) malloc( lp->im * sizeof(double) );
	lp->dsval = (double *) malloc( lp->im * sizeof(double) );
	lp->dsind = (int *) malloc( lp->im * sizeof(int) );
	lp->colstat = (int *) malloc( lp->in * sizeof(int) );
	lp->basisheader = (int *) malloc( lp->im * sizeof(int) );
	lp->etacol = (int *) malloc(lp->etamax * sizeof(int));
	lp->etastart = (int *) malloc(lp->etamax * sizeof(int));
	lp->etacount = (int *) malloc(lp->etamax * sizeof(int));
	lp->eta = (double *) malloc(lp->etamax * sizeof(double));
	lp->etaval = (double *) malloc(lp->etamax * lp->im * sizeof(double));
	lp->etaind = (int *) malloc(lp->etamax * lp->im * sizeof(int));
	if (!lp->x ||!lp->redcost ||!lp->y ||!lp->ysval ||!lp->ysind 
		|| !lp->d || !lp->dsval || !lp->dsind || !lp->colstat 
		|| !lp->basisheader ||!lp->etacol ||!lp->etastart
		|| !lp->etacount ||!lp->eta || !lp->etaval ||!lp->etaind)
		// run out of memory
	{
		fprintf (stderr, "run out of memory\n");
		return(ALPS_RUNOUTOFMEMORY);
	}

	/** Construct initial basis */
	/*
	*  Here we set up the initial basis for phase~1. At present, there are
	* three options. The first is to proceed along the same lines as Bixby
	* in his {\sl ORSA Journal on Computing} publication. The initial basis
	* consists of all slacks, some structural variables and some
	* artificials. The second option is a simplified version where
	* structurals are chosen such that the basismatrix is triangular. The
	* third option does not introduce structural basic variables. Variables
	* not selected are nonbasic, i.e., either free or at their upper or at
	* their lower bound. Along with this, the corresponding solution vector
	* |lp->x| is set up for the nonbasic variables.
	*/
	// Initializations

	for (i=0; i<lp->im; i++) {
		rowcovered[i] = ALPS_FALSE;
		rowpivot[i] = ALPS_REAL_INFINITY;
		rowfill[i] = 0;
	}
	for (j=0; j<lp->in; j++) {
		lp->colstat[j] = ALPS_UNKNOWN;
		lp->iphase1obj[j] = 0.0;
		lp->ioriglowerbound[j] = lp->ilowerbound[j]; 
		lp->iorigupperbound[j] = lp->iupperbound[j]; 
		lp->origvarstat[j] = lp->varstat[j]; 
		lp->iartifbounds[j] = ALPS_FALSE; 
	}
	initbasisslacks = 0;
	initbasisstructs = 0;
	initbasisartifs = 0;


	switch (lp->initialbasis) {
  case ALPS_INITBASIS:
	  // Select all slack variables
	  /*
	  *  The following makes all slack variables basic.
	  */
	  for (j=lp->inorig;j<lp->inonartif;j++) {
		  row_ind_of_slack_art = lp->imatcolind[lp->imatcolbeg[j]];
		  lp->colstat[j] = ALPS_BASIC;
		  lp->basisheader[j-lp->inorig] = j;
		  rowcovered[row_ind_of_slack_art] = ALPS_TRUE;
		  rowfill[row_ind_of_slack_art] = 1;
	  }
	  nextbasisheader = lp->inonartif - lp->inorig;
	  initbasisslacks = lp->inonartif-lp->inorig;
	  if (nextbasisheader>=lp->im) goto done;


	  // Priorities for structural variables
	  /*
	  *  We will later scan the structural variables for checking if they are
	  * inserted into the initial basis. We scan these variables in a sequence
	  * according to priorities which are based primarily on their bounds and
	  * secondarily on their objective function coefficients.
	  */
	  freecount = 0;
	  oneboundcount = 0;
	  twoboundscount = 0;
	  maxabsobjcoeff = -ALPS_REAL_INFINITY;
	  for (j=0; j<lp->inorig; j++) {
		  absobjcoeff = fabs(lp->iphase2obj[j]);
		  if (absobjcoeff>maxabsobjcoeff)
			  maxabsobjcoeff = absobjcoeff;
		  if (lp->varstat[j]==ALPS_BOUNDEDTWICE)
			  twoboundscount++;
		  else if ((lp->varstat[j]==ALPS_BOUNDEDABOVE)
			  ||(lp->varstat[j]==ALPS_BOUNDEDBELOW)) 
			  oneboundcount++;
		  else if (lp->varstat[j]==ALPS_FREE) freecount++;
	  }
	  if (maxabsobjcoeff<ALPS_ZEROEPS) maxabsobjcoeff = 1.0;
	  else maxabsobjcoeff *= 1000.0;
	  freepos = 0;
	  oneboundpos = freecount;
	  twoboundspos = freecount + oneboundcount;
	  for (j=0; j<lp->inorig; j++) {
		  objcorr = - lp->iphase2obj[j]/maxabsobjcoeff;
		  if (lp->varstat[j]==ALPS_BOUNDEDTWICE) {
			  sortindex[twoboundspos] = j;
			  sortkey[twoboundspos++] = lp->ilowerbound[j] - lp->iupperbound[j] + objcorr;
		  }
		  else if (lp->varstat[j]==ALPS_BOUNDEDBELOW) {
			  sortindex[oneboundpos] = j;
			  sortkey[oneboundpos++] = lp->ilowerbound[j] + objcorr;
		  }
		  else if (lp->varstat[j]==ALPS_BOUNDEDABOVE) {
			  sortindex[oneboundpos] = j;
			  sortkey[oneboundpos++] = -lp->iupperbound[j] + objcorr;
		  }
		  else if (lp->varstat[j]==ALPS_FREE) {
			  sortindex[freepos] = j;
			  sortkey[freepos++] = objcorr;
		  }
	  }
	  quick_sort2(sortkey,sortindex,0,freepos-1);
	  quick_sort2(sortkey,sortindex,freepos,oneboundpos-1);
	  quick_sort2(sortkey,sortindex,oneboundpos,twoboundspos-1);


	  // Select structural basic variables
	  /*
	  *  The following procedure insert basic structural variables allowing
	  * for slight violations of triangularity.
	  */
	  for (jj=0; jj<twoboundspos; jj++)  {
		  j = sortindex[jj];
		  if (lp->imatcolcount[j]) {
			  colrmax = 0.0;
			  colimax = 0.0;
			  colmax = 0.0;
			  colrind = -1;
			  coliind = -1;
			  for (k=lp->imatcolbeg[j]; k<lp->imatcolbeg[j]+lp->imatcolcount[j];k++) {
				  i = lp->imatcolind[k];
				  faij = fabs(lp->imatcolcoeff[k]);
				  if (faij>colmax) colmax = faij;
				  if (rowfill[i]==0) {
					  if (faij>colrmax) {
						  colrmax = faij;
						  colrind = i;
					  }
				  }
				  if (!rowcovered[i]) {
					  if (faij>colimax) {
						  colimax = faij;
						  coliind = i;
					  }
				  }
			  }
			  if (colmax<0.001) goto next;

			  if (colrmax>0.99*colmax) {
				  initbasisstructs++;
				  lp->colstat[j] = ALPS_BASIC;
				  lp->basisheader[nextbasisheader++] = j;
				  rowcovered[colrind] = ALPS_TRUE;
				  rowpivot[colrind] = colrmax;
				  for (k=lp->imatcolbeg[j]; k<lp->imatcolbeg[j]+lp->imatcolcount[j];k++) {
					  i = lp->imatcolind[k];
					  if (fabs(lp->imatcolcoeff[k])>ALPS_ZEROEPS) rowfill[i]++;
				  }
				  if (nextbasisheader>=lp->im) goto done;
				  goto next;
			  }

			  for (k=lp->imatcolbeg[j]; k<lp->imatcolbeg[j]+lp->imatcolcount[j];k++) {
				  i = lp->imatcolind[k];
				  faij = fabs(lp->imatcolcoeff[k]);
				  if (faij>0.01*rowpivot[i]) goto next;
			  }

			  if (colimax<0.001) goto next;

			  initbasisstructs++;
			  lp->colstat[j] = ALPS_BASIC;
			  lp->basisheader[nextbasisheader++] = j;
			  rowcovered[coliind] = ALPS_TRUE;
			  rowpivot[coliind] = colimax;
			  for (k=lp->imatcolbeg[j]; k<lp->imatcolbeg[j]+lp->imatcolcount[j];k++) {
				  i = lp->imatcolind[k];
				  if (fabs(lp->imatcolcoeff[k])>ALPS_ZEROEPS) rowfill[i]++;
			  }
			  if (nextbasisheader>=lp->im) goto done;
			  goto next;
		  }
next:;
	  }


	  break;
  case ALPS_TRIANGULAR_INITBASIS:
	  // Select all slack variables
	  /*
	  *  The following makes all slack variables basic.
	  */
	  for (j=lp->inorig;j<lp->inonartif;j++) {
		  row_ind_of_slack_art = lp->imatcolind[lp->imatcolbeg[j]];
		  lp->colstat[j] = ALPS_BASIC;
		  lp->basisheader[j-lp->inorig] = j;
		  rowcovered[row_ind_of_slack_art] = ALPS_TRUE;
		  rowfill[row_ind_of_slack_art] = 1;
	  }
	  nextbasisheader = lp->inonartif - lp->inorig;
	  initbasisslacks = lp->inonartif-lp->inorig;
	  if (nextbasisheader>=lp->im) goto done;


	  // Priorities for structural variables
	  /*
	  *  We will later scan the structural variables for checking if they are
	  * inserted into the initial basis. We scan these variables in a sequence
	  * according to priorities which are based primarily on their bounds and
	  * secondarily on their objective function coefficients.
	  */
	  freecount = 0;
	  oneboundcount = 0;
	  twoboundscount = 0;
	  maxabsobjcoeff = -ALPS_REAL_INFINITY;
	  for (j=0; j<lp->inorig; j++) {
		  absobjcoeff = fabs(lp->iphase2obj[j]);
		  if (absobjcoeff>maxabsobjcoeff)
			  maxabsobjcoeff = absobjcoeff;
		  if (lp->varstat[j]==ALPS_BOUNDEDTWICE)
			  twoboundscount++;
		  else if ((lp->varstat[j]==ALPS_BOUNDEDABOVE)
			  ||(lp->varstat[j]==ALPS_BOUNDEDBELOW)) 
			  oneboundcount++;
		  else if (lp->varstat[j]==ALPS_FREE) freecount++;
	  }
	  if (maxabsobjcoeff<ALPS_ZEROEPS) maxabsobjcoeff = 1.0;
	  else maxabsobjcoeff *= 1000.0;
	  freepos = 0;
	  oneboundpos = freecount;
	  twoboundspos = freecount + oneboundcount;
	  for (j=0; j<lp->inorig; j++) {
		  objcorr = - lp->iphase2obj[j]/maxabsobjcoeff;
		  if (lp->varstat[j]==ALPS_BOUNDEDTWICE) {
			  sortindex[twoboundspos] = j;
			  sortkey[twoboundspos++] = lp->ilowerbound[j] - lp->iupperbound[j] + objcorr;
		  }
		  else if (lp->varstat[j]==ALPS_BOUNDEDBELOW) {
			  sortindex[oneboundpos] = j;
			  sortkey[oneboundpos++] = lp->ilowerbound[j] + objcorr;
		  }
		  else if (lp->varstat[j]==ALPS_BOUNDEDABOVE) {
			  sortindex[oneboundpos] = j;
			  sortkey[oneboundpos++] = -lp->iupperbound[j] + objcorr;
		  }
		  else if (lp->varstat[j]==ALPS_FREE) {
			  sortindex[freepos] = j;
			  sortkey[freepos++] = objcorr;
		  }
	  }
	  quick_sort2(sortkey,sortindex,0,freepos-1);
	  quick_sort2(sortkey,sortindex,freepos,oneboundpos-1);
	  quick_sort2(sortkey,sortindex,oneboundpos,twoboundspos-1);


	  // Simple selection of structural basic variables
	  /*
	  *  We insert structural basic variables as long as the basis matrix
	  * stays triangular.
	  */
	  for (jj=0; jj<twoboundspos; jj++)  {
		  j = sortindex[jj];
		  if (lp->imatcolcount[j]) {
			  for (k=lp->imatcolbeg[j]; k<lp->imatcolbeg[j]+lp->imatcolcount[j];k++) {
				  i = lp->imatcolind[k];
				  if (rowcovered[i]) goto nextvar;
			  }
			  initbasisstructs++;
			  lp->colstat[j] = ALPS_BASIC;
			  lp->basisheader[nextbasisheader++] = j;
			  if (nextbasisheader>=lp->im) goto done;
			  k = lp->imatcolbeg[j]; 
			  i = lp->imatcolind[k];
			  rowcovered[i] = ALPS_TRUE;
		  }
nextvar:;
	  }


	  break;
  case ALPS_ARTIFICIAL_INITBASIS:
	  // Select all slack variables
	  /*
	  *  The following makes all slack variables basic.
	  */
	  for (j=lp->inorig;j<lp->inonartif;j++) {
		  row_ind_of_slack_art = lp->imatcolind[lp->imatcolbeg[j]];
		  lp->colstat[j] = ALPS_BASIC;
		  lp->basisheader[j-lp->inorig] = j;
		  rowcovered[row_ind_of_slack_art] = ALPS_TRUE;
		  rowfill[row_ind_of_slack_art] = 1;
	  }
	  nextbasisheader = lp->inonartif - lp->inorig;
	  initbasisslacks = lp->inonartif-lp->inorig;
	  if (nextbasisheader>=lp->im) goto done;


	  break;
  default:
	  // Select all slack variables
	  /*
	  *  The following makes all slack variables basic.
	  */
	  for (j=lp->inorig;j<lp->inonartif;j++) {
		  row_ind_of_slack_art = lp->imatcolind[lp->imatcolbeg[j]];
		  lp->colstat[j] = ALPS_BASIC;
		  lp->basisheader[j-lp->inorig] = j;
		  rowcovered[row_ind_of_slack_art] = ALPS_TRUE;
		  rowfill[row_ind_of_slack_art] = 1;
	  }
	  nextbasisheader = lp->inonartif - lp->inorig;
	  initbasisslacks = lp->inonartif-lp->inorig;
	  if (nextbasisheader>=lp->im) goto done;


	  break;
	}
	// Complete with artificial variables
	/*
	*  For uncovered rows we insert the corresponding artificial variables
	* into the basis.
	*/
	for (j=lp->inonartif;j<lp->in;j++) {
		row_ind_of_slack_art = lp->imatcolind[lp->imatcolbeg[j]];
		if (!rowcovered[row_ind_of_slack_art]) {
			initbasisartifs++;
			lp->colstat[j] = ALPS_BASIC;
			lp->basisheader[nextbasisheader++] = j;
			if (nextbasisheader>=lp->im) goto done;
		}
	}
done:


	// Set nonbasic x-vector
	/*
	*  We now set the remaining structural and artificial nonbasic
	* variables. Depending on their coefficient in the phase~2 objective
	* function setting to lower or upper bound is preferred if possible.
	* Nonbasic variables are always set to feasible values. We already
	* compute the right hand side with which to compute the values of the
	* basic variables.
	*/
	for (i=0; i<lp->im; i++) trhs[i] = lp->irhs[i];
	for (j=0;j<lp->inorig;j++) {
		if (lp->colstat[j]==ALPS_UNKNOWN) {
			if (lp->varstat[j]==ALPS_BOUNDEDTWICE) {
				if (lp->iphase2obj[j]>0.0) {
					lp->colstat[j] = ALPS_NONBASICUPB;
					lp->x[j] = lp->iupperbound[j];
				}
				else {
					lp->colstat[j] = ALPS_NONBASICLOWB;
					lp->x[j] = lp->ilowerbound[j];
				}
			}
			else if (lp->varstat[j]==ALPS_BOUNDEDBELOW) {
				lp->colstat[j] = ALPS_NONBASICLOWB;
				lp->x[j] = lp->ilowerbound[j];
			}
			else if (lp->varstat[j]==ALPS_BOUNDEDABOVE) {
				lp->colstat[j] = ALPS_NONBASICUPB;
				lp->x[j] = lp->iupperbound[j];
			}
			else if (lp->varstat[j]==ALPS_FIXED) {
				lp->colstat[j] = ALPS_NONBASICLOWB;
				lp->x[j] = lp->ilowerbound[j];
			}
			else if (lp->varstat[j]==ALPS_FREE) {
				lp->colstat[j] = ALPS_NONBASICFREE;
				lp->x[j] = 0.0;
			}
			for (k=lp->imatcolbeg[j]; k<lp->imatcolbeg[j]+lp->imatcolcount[j];k++) {
				i = lp->imatcolind[k];
				trhs[i] -= lp->imatcolcoeff[k]*lp->x[j];
			}
		}
	}
	for (j=lp->inonartif;j<lp->in;j++) {
		if (lp->colstat[j]==ALPS_UNKNOWN) {
			lp->colstat[j] = ALPS_NONBASICLOWB;
			lp->x[j] = 0.0;
		}
	}
	for (j=0;j<lp->in;j++) if (lp->colstat[j]!=ALPS_BASIC) { double viol;
	viol = lp->x[j] - lp->iupperbound[j];
	if (viol>0.0001) {
		printf("\n0Strong upper bound violation of variable %d: %lf > %lf!\n",
			j,lp->x[j],lp->iupperbound[j]);
		exit(1000);
	}
	viol = lp->ilowerbound[j] - lp->x[j];
	if (viol>0.0001) {
		printf("\n0Strong lower bound violation of variable %d: %lf < %lf!\n",
			j,lp->x[j],lp->ilowerbound[j]);
		exit(1000);
	}
	}


	if (pril>=1) {
		printf("Initial basis: %1d original vars, %1d slacks, %1d artificials.\n",
			initbasisstructs,initbasisslacks,initbasisartifs);
	}


	/** Compute basic solution */

	ffree( (char **) &lp->basmatbeg);
	ffree( (char **) &lp->basmatcount);
	ffree( (char **) &lp->basmatind);
	ffree( (char **) &lp->basmatcoeff);
	basnonz = 0;
	for(k=0;k<lp->im;k++) basnonz += lp->imatcolcount[lp->basisheader[k]];
	lp->basmatbeg = (int *) malloc( lp->im * sizeof(int) );
	lp->basmatcount = (int *) malloc( lp->im * sizeof(int) );
	lp->basmatind = (int *) malloc( basnonz * sizeof(int) );
	lp->basmatcoeff = (double *) malloc( basnonz * sizeof(double) );
	if (!lp->basmatbeg || ! lp->basmatcount || !lp->basmatind 
		|| !lp->basmatcoeff )
		// run out of memory

	{
		fprintf (stderr, "run out of memory\n");
		return(ALPS_RUNOUTOFMEMORY);
	}



	ind=0;
	for(k=0;k<lp->im;k++) {
		j = lp->basisheader[k];
		lp->basmatbeg[k] = ind;
		lp->basmatcount[k] = lp->imatcolcount[j];
		for (l=lp->imatcolbeg[j];l<lp->imatcolbeg[j]+lp->imatcolcount[j];l++) {
			lp->basmatind[ind] = lp->imatcolind[l];
			lp->basmatcoeff[ind] =  lp->imatcolcoeff[l];
			ind++;
		}
	}

	ffree( (char **) &pivrow);
	ffree( (char **) &pivcol);
	ffree( (char **) &pivvalue);
	ffree( (char **) &lp->rumatbeg);
	ffree( (char **) &lp->rumatend);
	ffree( (char **) &lp->rumatind);
	ffree( (char **) &lp->rumatcoeff);
	ffree( (char **) &lp->clmatbeg);
	ffree( (char **) &lp->clmatend);
	ffree( (char **) &lp->clmatind);
	ffree( (char **) &lp->clmatcoeff);
	ffree( (char **) &lp->cumatbeg);
	ffree( (char **) &lp->cumatend);
	ffree( (char **) &lp->cumatind);
	ffree( (char **) &lp->cumatcoeff);
#ifdef ALPS_TIMING_ENABLED
	starttime = cputime();
#endif
	status= alps_lufac (
		pril,
		(int) 0,
		lp->im,
		lp->basmatbeg,
		lp->basmatcount,
		lp->basmatind,
		lp->basmatcoeff,
		&pivrow,
		&pivcol,
		&pivvalue,
		&lp->rumatbeg,
		&lp->rumatend,
		&lp->rumatind,
		&lp->rumatcoeff,
		&lp->cumatbeg,
		&lp->cumatend,
		&lp->cumatind,
		&lp->cumatcoeff,
		&lp->clmatbeg,
		&lp->clmatend,
		&lp->clmatind,
		&lp->clmatcoeff,
		&totalfill,
		&totalcancel
		);
#ifdef ALPS_TIMING_ENABLED
	stoptime = cputime();
	lp->factortime += (stoptime - starttime);
#endif
	if (status==ALPS_RUNOUTOFMEMORY) {
		fprintf(stderr,"LU factorization failed: run out of memory.\n");
		return ALPS_RUNOUTOFMEMORY;
	}
	else if (status) {
		fprintf(stderr,"LU factorization failed. Matrix singular.\n");
		return ALPS_LU_NOPIVOT;
	}

#ifdef ALPS_TIMING_ENABLED
	starttime = cputime();
#endif
	status = alps_fsolveeqs(
		lp,
		lp->im,
		pivrow,
		pivcol,
		pivvalue,
		lp->rumatbeg,
		lp->rumatend,
		lp->rumatind,
		lp->rumatcoeff,
		lp->clmatbeg,
		lp->clmatend,
		lp->clmatind,
		lp->clmatcoeff,
		lp->etanr,
		lp->etacol,
		lp->etaval,
		lp->etaind,
		lp->etastart,
		lp->etacount,
		lp->eta,
		trhs,
		xbas
		);

#ifdef ALPS_TIMING_ENABLED
	stoptime = cputime();
	lp->fsolvetime += (stoptime - starttime);
#endif
	if (status==ALPS_RUNOUTOFMEMORY) {
		fprintf(stderr,"FSOLVE failed: run out of memory.\n");
		exit(ALPS_RUNOUTOFMEMORY);
	}


	/** Set bounds and construct phase1 objective function */
	/*
	*  We temporarily set the bounds of the basic variables so as to
	* accomodate their current values. The phase 1 objective function
	* punishes infeasibilities. A slack variable either receives objective
	* function coefficient |0.0|, if it is already nonnegative, or else
	* |1.0| to count this infeasibility. A basic structural or artificial
	* variable variable receives |-1.0|, |0.0|, or |1.0|, depending on
	* whether it is above its upperbound, is within its bounds, or below its
	* lower bound, respectively. To avoid degeneracies the true values are
	* perturbed slightly at random. We switch to this temporary objective
	* function, and compute the initial objective function value.
	*/

	if (pril>=4) printprimalsol(lp);
	randseed = 333333;
	lp->objval = 0.0;
	for (i=0; i<lp->im; i++) {
		j = lp->basisheader[i];
		lp->x[j] = xbas[i];
		if (lp->x[j]<lp->ilowerbound[j]-ALPS_ZEROEPS) {
			lp->iupperbound[j] = lp->ilowerbound[j];
			lp->ilowerbound[j] = -ALPS_REAL_INFINITY;
			lp->iartifbounds[j] = ALPS_TRUE;
			lp->varstat[j] = ALPS_BOUNDEDABOVE;
			lp->iphase1obj[j] = 1.0 + 0.2*rand01(&randseed);
			lp->objval -= lp->iphase1obj[j]*(lp->ioriglowerbound[j]-lp->x[j]);
		}
		else if (lp->x[j]>lp->iupperbound[j]+ALPS_ZEROEPS) {
			lp->ilowerbound[j] = lp->iupperbound[j];
			lp->iupperbound[j] = ALPS_REAL_INFINITY;
			lp->iartifbounds[j] = ALPS_TRUE;
			lp->varstat[j] = ALPS_BOUNDEDBELOW;
			lp->iphase1obj[j] = -1.0 - 0.2*rand01(&randseed);
			lp->objval += lp->iphase1obj[j]*(lp->x[j]-lp->iorigupperbound[j]);
		}
	}
	lp->iobj = lp->iphase1obj;

	lp->basisstatus=ALPS_PHASE1BASIS; 
	if (pril>=4) printprimalsol(lp);


	/** Free space for alps\_phase1prim arrays */

	ffree( (char **) &rowcovered);
	ffree( (char **) &rowfill);
	ffree( (char **) &rowpivot);
	ffree( (char **) &sortkey);
	ffree( (char **) &sortindex);
	ffree( (char **) &pivrow);
	ffree( (char **) &pivcol);
	ffree( (char **) &pivvalue);
	ffree( (char **) &xbas);
	ffree( (char **) &trhs);

	return 0;
}