Exemple #1
0
/* deadxc is also NOT used by FourSort.  It is employed for 
   a test to invoke cut3duplicates */
void deadxc(int N, int M, int depthLimit) {
  if ( depthLimit <= 0 ) {
    heapc(A, N, M);
    return;
  }
  int L = M - N;  
  if ( L < cut3Limit ) { 
    quicksort0c(N, M, depthLimit);
    return;
  }
  depthLimit--;

    int pivotx;
    int midpointx = (M+N)/2;
    if ( L <= 30 ) { // being defensive
      pivotx = med(A, N, midpointx, M, compareXY);
    } else {
      int step = L * 0.1;
      int P = med(A, N +   step, N + 2*step, N + 3*step, compareXY);
      int Q = med(A, midpointx - step, midpointx, midpointx + step, compareXY);
      int R = med(A, M - 3*step, M - 2*step, M - step, compareXY);
      pivotx = med(A, P, Q, R, compareXY);
    }

    void deadxc();
    cut3duplicates(N, M, pivotx, deadxc, depthLimit);
} // end deadxc
Exemple #2
0
// Cut2c does 2-partitioning with one pivot.
// Cut2c invokes dflgm when trouble is encountered.
void cut2c(void **A, int N, int M, int depthLimit, int (*compareXY)()) {
  int L;
 Start:
  L = M - N + 1;
  if ( L <= 1 ) return;

  // /*
  if ( L < 8 ) { // insertionsort
    insertionsort(A, N, M, compareXY);
    return;
  }
  //  */

  if ( depthLimit <= 0 ) {
    heapc(A, N, M, compareXY);
    return;
  }
  depthLimit--;

  // /*
  if ( L < cut2Limit ) { 
    // This alternative over esaping to quicksort0c reduced 1/2% comparions
    int middlex = N + (L>>1); // N + L/2;
    int p0 = middlex;
    if ( 7 < L ) {
      int pn = N;
      int pm = M;
      if ( 51 < L ) {	
	int d = (L-2)>>3; // L/8;
	pn = med(A, pn, pn + d, pn + 2 * d, compareXY);
	p0 = med(A, p0 - d, p0, p0 + d, compareXY);
	pm = med(A, pm - 2 * d, pm - d, pm, compareXY);
      }
Exemple #3
0
int comm(int a[],int m,int l){
    int i=0,p=0,c=0;
    int q= l-1;
    int sa[p],sb[l-p];
    
    
    if(l>2){
        for(i=0;i<l;i++){
           if(a[i]<=m){         
              sa[p] = a[i];     //Array contenete valori <= m
               p = p + 1;
        }else{
              sb[c] = a[i];     //Array contenete valori > m
              c = c + 1;
              q= q -1;
        }
      }

    int la = p;                 //length[sa]
    int lb = l-p;               //length[sb]
     comm(sa,med(sa,la),la);
     comm(sb,med(sb,lb),lb);

    
    }else if(l==2 && a[0]==a[1]){   //Numero comune
        printf("Common values are: ");
        printf("%d ",a[0]);
        }
        return 0;
}
Exemple #4
0
int main()
{
int *array1,*array2,i,n,m,med1,med2,p=0,q=0;
printf("\nenter the sizes of array first and second arrays");
scanf("%d %d",&m,&n);
array1=(int*)malloc(sizeof(int)*m);
array2=(int*)malloc(sizeof(int)*n);
printf("\nenter the first sorted array");
for(i=0;i<m;i++)
{
scanf("%d",&array1[i]);
}
printf("\nenter the second sorted array");
for(i=0;i<n;i++)
{
scanf("%d",&array2[i]);
}
label1:
med1=med(array1,p,m);
med2=med(array2,q,n);
if(m>=1 && n>=1)
{
if(med1==med2)
{
printf("\n%d is the median ",med1);
return 0;
}
else if(med1>med2)
{

m=m/2; 
 q=n/2; 
goto label1;
}
else 
{
	p=m/2; 
    n=n/2; 

goto label1;
}
}
else
{
med1=array1[p]+array2[q]+array1[m]+array2[n]-max(array1[p],array2[q],array1[m],array2[n])-min(array1[p],array2[q],array1[m],array2[n]) / 2;
printf("\n%d is the median ",med1);
}
return 0;
}
      void median(const Basic2DArray<T>& a, Basic2DArray<T>& b){
      
      Vector<T> med(9) ; 
      b.resize(a.rows(),a.cols());
      for(int i=a.rows()-2;i>0;--i)
	for(int j=a.cols()-2;j>0;--j){
	  int n = -1 ; 
	  for(int k=-1;k<2;++k)
	    for(int l=-1;l<2;++l){
	      med[++n] = a(i+k,j+l) ;
	    }
	  med.qSort();
	  b(i,j) = med[4] ; 
	}
      // have to handle the borders
      for(int i=0;i<a.rows();++i){
	b(i,0) = b(i,1) ; 
	b(i,b.cols()-1) = b(i,b.cols()-2) ;
      }
      for(int i=0;i<a.cols();++i){
	b(0,i) = b(1,i) ; 
	b(b.rows()-1,i) = b(b.rows()-2,i) ;
      }
      // and handling the corners
      b(0,0) = b(1,1) ; 
      b(0,b.cols()-1) = b(1,b.cols()-2) ; 
      b(b.rows()-1,0) = b(b.rows()-2,1) ; 
      b(b.rows()-1,b.cols()-1) = b(b.rows()-2,b.cols()-2) ;           
    }
Exemple #6
0
int main()
{
    int x,y,i;

    printf("Length of the arrays A and B: ");
    scanf("%d %d", &x, &y);
    int a[x];
    int b[y];
    int c[x+y];
    
    printf("A array values: ");
    for(i=0;i<x;i++){
        scanf("%d",&a[i]);
    }

    printf("B array values: ");
    for(i=0;i<y;i++){
        scanf("%d",&b[i]);
    }
    
    //-----Merge A and B into C--------
    
    for(i=0;i<=x-1;i++){
      c[i] = a[i];
    }
    for(i=0;i<=y-1;i++){
      c[i+x] = b[i];
    }
    
    int l = x +y;        // length[c]
    comm(c,med(c,l),l);

    return 0;
}
      void medianT(const Basic2DArray<T>& a, Basic2DArray<T>& b, T value, int op){
      
      Vector<T> med(9) ; 
      b.resize(a.rows(),a.cols());

      if(op>0){

	for(int i=a.rows()-2;i>0;--i)
	  for(int j=a.cols()-2;j>0;--j){
	    int n = -1 ; 
	    for(int k=-1;k<2;++k)
	      for(int l=-1;l<2;++l){
		med[++n] = a(i+k,j+l) ;
	      }
	    med.qSort();
	    --n ;
	    while(n>0 && med[n]>=value){
	      --n ;
	    }
	    b(i,j) = med[n/2] ; 
	  }
      }
      else{

	for(int i=a.rows()-2;i>0;--i)
	  for(int j=a.cols()-2;j>0;--j){
	    int n = -1 ; 
	    for(int k=-1;k<2;++k)
	      for(int l=-1;l<2;++l){
		med[++n] = a(i+k,j+l) ;
	      }
	    med.qSort();
	    n=0;
	    while(n>0 && med[n]<=value){
	      ++n ;
	    }
	    b(i,j) = med[n+(9-n)/2] ; 
	  }
      }
      // have to handle the borders
      for(int i=0;i<a.rows();++i){
	b(i,0) = b(i,1) ; 
	b(i,b.cols()-1) = b(i,b.cols()-2) ;
      }
      for(int i=0;i<a.cols();++i){
	b(0,i) = b(1,i) ; 
	b(b.rows()-1,i) = b(b.rows()-2,i) ;
      }
      // and handling the corners
      b(0,0) = b(1,1) ; 
      b(0,b.cols()-1) = b(1,b.cols()-2) ; 
      b(b.rows()-1,0) = b(b.rows()-2,1) ; 
      b(b.rows()-1,b.cols()-1) = b(b.rows()-2,b.cols()-2) ;           
    }
Exemple #8
0
int fractal (float x1, float y1,float x2,float y2,float wf,int it){
float xm,ym,dx,dy,G;
if(it==0)
	return 0;
xm=med(x1,x2);
ym=med(y1,y2);
G=getrandom();
dx=L*G*wf;
xm+=dx;
G=getrandom();
dy=L*G*wf;
ym+=dy;
setcolor(8);
line(x1,y1,x2,y2);
setcolor(WHITE);
line(x1,y1,xm,ym);
line(xm,ym,x2,y2);
//getch();
fractal(x1,y1,xm,ym,wf,it-1);
fractal(xm,ym,x2,y2,wf,it-1);
return 0;
}
Exemple #9
0
void EmulApp::setupAppIcon()
{
#include "FSM.xpm"    
#include "FSM_smaller.xpm"    
#include "FSM_med.xpm"
    QPixmap lg(FSM_xpm), sm(FSM_smaller_xpm), med(FSM_med_xpm);
    QIcon ic(med);

    ic.addPixmap(lg);
    ic.addPixmap(sm);
    
    mainwin->setWindowIcon(ic);    
    controlwin->setWindowIcon(ic);
    pfv->setWindowIcon(ic);
    mpv->setWindowIcon(ic);
}
Exemple #10
0
// This method does as much as it can in one pass through of the table, converting all rows to columns, and calculating the minimum, maximum, and average along the way. The median as saved for later if requested, since it requires a sort.
void CSVAggregator::aggregate(CSVTable &t, Config &config) {
    for (int i = 0; i < t.column_count; i++) {
        CSVColumn column;
        t.columns.push_back(column);
    }
    for (int j = 0; j < t.rows[0].size(); j++) {
        t.columns[j].header = t.rows[0][j];
    }
    for (int i = 1; i < t.rows.size(); i++) {
        for (int j = 0; j<t.rows[i].size(); j++) {
            std::string el = t.rows[i][j];
            t.columns[j].push_back(el);

            TextParser::strip_quotes(el);
            int val = atoi(el.c_str());
            t.columns[j].aggregator.sum += val;
            t.columns[j].aggregator.push_back(val);
            
            // This is a new aggregator (or it has been reset) - we assume the first value passed is both the min and the max
            if(t.columns[j].aggregator.is_clean == true) {
                t.columns[j].aggregator.min = val;
                t.columns[j].aggregator.max = val;
                t.columns[j].aggregator.is_clean = false;
            } else {
                if(val < t.columns[j].aggregator.min) {
                    t.columns[j].aggregator.min = val;
                }
                if(val > t.columns[j].aggregator.max) {
                    t.columns[j].aggregator.max = val;
                }
            }
        }
    }

    // After all the values have been loaded in, we can compute the average and median
    for (int i = 0; i < t.columns.size(); i++) {
        t.columns[i].aggregator.avg = t.columns[i].aggregator.sum / t.columns[i].aggregator.vals.size();

        // Only do this if requested
        if (config.med == true) {
            med(t.columns[i], config);
        }
    }
}
Exemple #11
0
int
main (int argc, char **argv)
{
  char *run1, *run2, *qrels = (char *) 0;

  setProgramName (argv[0]);

  if (argc == 3 || argc == 4)
    {
      run1 = argv[1];
      run2 = argv[2];
      if (argc == 4)
        qrels= argv[3];
    }
  else
    usage ();

  computeRelevanceProbabilities ();
  med (run1, run2, qrels);

  return 0;
}
Exemple #12
0
// Quicksort equipped with a defense against quadratic explosion;
// calling heapsort if depthlimit exhausted
void quicksort0c(int N, int M, int depthLimit) {
  // printf("Enter quicksort0c N: %d M: %d %d\n", N, M, depthLimit);
  while ( N < M ) {
    int L = M - N;
    if ( L <= 10 ) {
      insertionsort(N, M);
      return;
    }
    if ( depthLimit <= 0 ) {
      heapc(A, N, M);
      return;
    }
    depthLimit--;
    // 10 < L
    // grab median of 3 or 9 to get a good pivot
    int pn = N;
    int pm = M;
    int p0 = (pn+pm)/2;
    if ( 40 < L ) { // do median of 9
      int d = L/8;
      pn = med(A, pn, pn + d, pn + 2 * d, compareXY);
      p0 = med(A, p0 - d, p0, p0 + d, compareXY);
      pm = med(A, pm - 2 * d, pm - d, pm, compareXY);
      /* Activation of the check for duplicates gives a slow down of 
	 1/4% on uniform input.  If you suspect that duplicates
	 causing quadratic deterioration are not caught higher-up by cut3
	 you may want to experiment with this check::::
      if ( L < 100 ) { // check for duplicates
	int duplicate = -1;
	if ( compareXY(A[pn], A[pm]) == 0 ) { duplicate = pn; } else
	if ( compareXY(A[pn], A[p0]) == 0 ) { duplicate = pn; } else
	if ( compareXY(A[pm], A[p0]) == 0 ) { duplicate = pm; };
	if ( 0 < duplicate ) {
	  void quicksort0();
	  cut3duplicates(N, M, duplicate, quicksort0, depthLimit);
	  return;
	}
      }
      */
    }
    p0 = med(A, pn, p0, pm, compareXY); // p0 is index to 'best' pivot ...
    iswap(N, p0, A); // ... and is put in first position as required by quicksort0c

    register void *p = A[N]; // pivot
    register int i, j;
    i = N;
    j = M;
    register void *ai; void *aj;

    /* Split array A[N,M], N<M in two segments using pivot p; 
       construct a partition with A[N,i), A(i,M] and N <= i <= M, and
       N <= k <= i -> A[k] <= p  and  i < k <= M -> p < A[k];
       Allow the worse cases: N=i or i=M.
       Recurse on A[N,i) and A(i,M) (or in the reverse order).
       This code does NOT do swapping; instead it disposes 
       ai/aj in a hole created by setting aj/ai first.  
    */
    /* Start state:
	  |-------------------------------|
          N=i                           j=M
	  N = i < j = M
          N <= k < i -> A[k] <= p    
          N < j < k <= M -> p < A[k]
	  A[N] = p
          N < i -> p < A[i]
    */
    while ( i < j ) {
      /*
	  |-------o---------------[--------|
          N       i               j        M
	  N <= i < j <= M
          N <= k < i -> A[k] <= p    
          N < j < k <= M -> p < A[k]
	  A[N] <= p
          N < i -> p < A[i]
          p + A[N,i) + A(i,M] is a permutation of the input array
      */
      aj = A[j];
      while ( compareXY(p, aj) < 0 ) { 
        /*
	  |-------o---------------[--------|
          N       i               j        M
	  N = i < j <= M or N < i <= j <= M
          N <= k < i -> A[k] <= p    
          N < j <= k <= M -> p < A[k]
	  A[N] <= p
	  N < i -> p < A[i}
	  p + A[N,i) + A(i,M] is a permutation of the input array
          p < aj = A[j]
	*/
	j--; 
	aj = A[j]; 
      }
      /*
	  |-------o---------------[--------|
          N       i               j        M
	  N = i = j < M or N < i & = i-1 <= j <= M
          N <= k < i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  N < i -> p < A[i}
	  p + A[N,i) + A(i,M] is a permutation of the input array
          aj = A[j] <= p
	*/
      if ( j <= i ) {
	/*
	  |-------o-----------------------|
          N       i                       M
	  N = i = j < M or N < i & = i-1 = j < M
          N <= k < i -> A[k] <= p    
          i < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,i) + A(i,M] is a permutation of the input array
      */
	break;
      }
      // i < j 
      A[i] = aj; // fill hole !
      /*
	  |-------]---------------o--------|
          N       i               j        M
	  N <= i < j <= M
          N <= k <= i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,j) + A(j,M] is a permutation of the input array
	  aj = A[j] <= p
      */
      i++; ai = A[i];
      while ( i < j && compareXY(ai, p) <= 0 ) {
      /*
	  |-------]---------------o--------|
          N       i               j        M
	  N < i < j <= M
          N <= k <= i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,j) + A(j,M] is a permutation of the input array
	  aj = A[j] <= p
      */
	i++; ai = A[i]; 
      }
      if ( j <= i ) 
      /*
	  |----------------------o--------|
          N                     i=j       M
	  N < i = j <= M
          N <= k < i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
	  p + A[N,j) + A(j,M] is a permutation of the input array
      */
	break;
      // i < j  & p < ai = A[i] 
      A[j] = ai;
      j--;
      /*
	  |--------o--------------[--------|
          N        i              j        M
	  N < i <= j <= M
          N <= k < i -> A[k] <= p    
          j < k <= M -> p < A[k]
	  A[N] <= p
          N < i -> p < ai = A[i]
	  p + A[N,i) + A(i,M] is a permutation of the input array
      */
    }
    A[i] = p;
    /*
	  |--------]----------------------|
          N        i                      M
	  N <= i <= M
          N <= k <= i -> A[k] <= p    
          i < k <= M -> p < A[k]
	  A[N] <= p
	  A[N,i] + A(i,M] is a permutation of the input array
    */
    // Recurse on the smallest one and iterate on the other one
    int ia = i-1; int ib = i+1; 
    if ( i-N < M-i ) { 
      if ( N < ia ) quicksort0c(N, ia, depthLimit);  
      N = ib; 
    } else { 
      if ( ib < M ) quicksort0c(ib, M, depthLimit);  
      M = ia; 
    }
  }
} // end of quicksort0c
Exemple #13
0
  // /*
  if ( L < cut2Limit ) { 
    // This alternative over esaping to quicksort0c reduced 1/2% comparions
    int middlex = N + (L>>1); // N + L/2;
    int p0 = middlex;
    if ( 7 < L ) {
      int pn = N;
      int pm = M;
      if ( 51 < L ) {	
	int d = (L-2)>>3; // L/8;
	pn = med(A, pn, pn + d, pn + 2 * d, compareXY);
	p0 = med(A, p0 - d, p0, p0 + d, compareXY);
	pm = med(A, pm - 2 * d, pm - d, pm, compareXY);
      }
      p0 = med(A, pn, p0, pm, compareXY);
    }
    if ( middlex != p0 ) iswap(p0, middlex, A);
    dflgm(A, N, M, middlex, cut2c, depthLimit, compareXY);
    return;
  }
  // */
  /*
  if ( L < cut2Limit ) { 
    quicksort0c(A, N, M, depthLimit, compareXY);
    return;
  }
  // */

  register void *T; // pivot
  register int I = N, J = M; // indices