Example #1
0
void heapSort2(int *data, int n) {
	// from:http://www.cnblogs.com/richselian/archive/2012/01/04/2311837.html
	int top = n;
	int bottom = n;
	int child = n;
	int i = child;
	int tmp;
	for (; bottom > 0; i = child) {
		tmp = data[child];
		int lchild = i * 2 + 1;
		int rchild = i * 2 + 2;
		if (lchild < bottom && tmp < data[lchild]) {
			child = lchild;
		}
		if (rchild < bottom && tmp < data[rchild]) {
			child = rchild;
		}
		if (i < child) {
			swap2(data, i, child);
		} else {
			if (top > 0) {
				child = --top;
			} else {
				swap2(data, 0, --bottom);
				child = 0;
			}
		}
	}
}
Example #2
0
void ssort2(char **a, int n, int depth,int* index)
{   int d, r, partval;
    int Diff=0;
    char **pa, **pb, **pc, **pd, **pl, **pm, **pn, *t;
    //~ if (n < 10) {
        //~ inssort(a, n, depth);
        //~ return;
    //~ }
    pl = a;
    pm = a + (n/2);
    pn = a + (n-1);
    if (n > 30) { // On big arrays, pseudomedian of 9
        d = (n/8);
        pl = med3(pl, pl+d, pl+2*d);
        pm = med3(pm-d, pm, pm+d);
        pn = med3(pn-2*d, pn-d, pn);
    }
    pm = med3(pl, pm, pn);
    Diff=pm-a;
    int *IndexA,*IndexB,*IndexC,*IndexD,*IndexN;
    swap2(a, pm);
    swap2Index(index,index+Diff);
    partval = ptr2char(a);
    pa = pb = a + 1;
    pc = pd = a + n-1;
    IndexA=IndexB=index+1;
    IndexC=IndexD=index+n-1;
    for (;;) {
        while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) {
            if (r == 0) { swap2(pa, pb);swap2Index(IndexA, IndexB);pa++;IndexA++; }
            pb++;
            IndexB++;
        }
        while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) {
            if (r == 0) { swap2(pc, pd); swap2Index(IndexC, IndexD);pd--;IndexD-- ;}
            pc--;
            IndexC--;
        }
        if (pb > pc) break;
        swap2(pb, pc);
        swap2Index(IndexB,IndexC);
        pb++;
        IndexB++;
        pc--;
        IndexC--;
    }
    pn = a + n;
    IndexN=index+n;
    r = min(pa-a, pb-pa);    vecswap2(a,  pb-r, r); vecswap2Index(index,IndexB-r,r);
    r = min(pd-pc, pn-pd-1); vecswap2(pb, pn-r, r); vecswap2Index(IndexB,IndexN-r,r);
    if ((r = pb-pa) > 1)
        ssort2(a, r, depth,index);
    if (ptr2char(a + r) != 0)
        ssort2(a + r, pa-a + pn-pd-1, depth+1,index+r);
    if ((r = pd-pc) > 1)
        ssort2(a + n-r, r, depth,index+n-r);
}
Example #3
0
void
read_descriptrace( FILE *fp, Suds_tag header, Suds_descriptrace *d, int swap, long **data, int *bufsiz ) {

	fread( &d->dt_name.network, sizeof( char ), 4, fp );
	fread( &d->dt_name.st_name, sizeof( char ), 5, fp );
	fread( &d->dt_name.component, sizeof( char ), 1, fp );
	fread( &d->dt_name.inst_type, sizeof( short ), 1, fp );
	fread( &d->begintime, sizeof( double ), 1, fp );
	fread( &d->localtime, sizeof( short ), 1, fp );
	fread( &d->datatype, sizeof( char ), 1, fp );
	fread( &d->descriptor, sizeof( char ), 1, fp );
	fread( &d->digi_by, sizeof( short ), 1, fp );
	fread( &d->processed, sizeof( short ), 1, fp );
	fread( &d->length, sizeof( long ), 1, fp );
	fread( &d->rate, sizeof( float ), 1, fp );
	fread( &d->mindata, sizeof( float ), 1, fp );
	fread( &d->maxdata, sizeof( float ), 1, fp );
	fread( &d->avenoise, sizeof( float ), 1, fp );
	fread( &d->numclip, sizeof( long ), 1, fp );
	fread( &d->time_correct, sizeof( double ), 1, fp );
	fread( &d->rate_correct, sizeof( float ), 1, fp );

	if( swap ) {
		swap2( &d->dt_name.inst_type, &d->dt_name.inst_type, 1 );
		swap8( &d->begintime, &d->begintime, 1 );
		swap2( &d->localtime, &d->localtime, 1 );
		swap2( &d->digi_by, &d->digi_by, 1 );
		swap2( &d->processed, &d->processed, 1 );
		swap4( &d->length, &d->length, 1 );
		swap4( &d->rate, &d->rate, 1 );
		swap4( &d->mindata, &d->mindata, 1 );
		swap4( &d->maxdata, &d->maxdata, 1 );
		swap4( &d->avenoise, &d->avenoise, 1 );
		swap4( &d->numclip, &d->numclip, 1 );
		swap8( &d->time_correct, &d->time_correct, 1 );
		swap4( &d->rate_correct, &d->rate_correct, 1 );
	}

	if( d->datatype != '2' ) {

		complain( 1, "Datatype %s not understood--skipping\n", d->datatype );
		fseek( fp, header.data_length_bytes, SEEK_CUR );

	} else {
		if( *bufsiz < header.data_length_bytes ) {
			reallot( long *, *data, d->length ); 
			*bufsiz = d->length * sizeof( long );
		}

		fread( *data, sizeof( long ), d->length, fp );

		if( swap ) {
			swap4( *data, *data, d->length );
		}
	}
Example #4
0
static 
Int32 split_group(UInt32 *a, int n, int depth,int offset,Int32 pivot,int *first)
{
  int r, partval;
  UInt32 *pa, *pb, *pc, *pd, *pa_old, *pd_old;
  Int32 pivot_pos, t;
  UChar *text_depth,*text_limit;

  /* --------- initialization ------------------------------------ */
  pivot_pos = a[pivot];       /* starting position in T[] of pivot */
  text_depth = Text+depth;
  text_limit = text_depth+offset;

  /* ------------------------------------------------------------- */
  /* In the following for() loop: */
  /* [pa ... pd] is the current working region,  */
  /* pb moves from pa towards pd  */
  /* pc moves from pd towards pa */
  /* ------------------------------------------------------------- */
  pa = a; pd = a + n-1;

  for(  ; pa!=pd && (text_depth<text_limit); text_depth++) {
    assert(pa<pd);
    /* ------ the pivot char is Text[pivot_pos+depth] where  */
    /* depth = text_depth-Text. This is text_depth[pivot_pos] */
    partval = text_depth[pivot_pos];
    /* ----- partition ------------  */
    pb = pa_old = pa; 
    pc = pd_old = pd; 
    for (;;) {
        while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) {
            if (r == 0) { swap2(pa, pb); pa++; }
            pb++;
        }
        while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) {
            if (r == 0) { swap2(pc, pd); pd--; }
            pc--;
        }
        if (pb > pc) break;
        swap2(pb, pc);
        pb++;
        pc--;
    }
    r = min((Int32*)pa-(Int32*)pa_old, (Int32*)pb-(Int32*)pa); vecswap2(pa_old, pb-r, r);
    r = min((Int32*)pd-(Int32*)pc, (Int32*)pd_old-(Int32*)pd); vecswap2(pb, pd_old+1-r, r);
    /* ------ compute new boundaries -----  */ 
    pa = pa_old + (pb-pa);     /* there are pb-pa chars < partval */
    pd = pd_old - (pd-pc);     /* there are pd-pc chars > partval */

  }
  *first=pa-a;        /* index in a[] of the first suf. equal to pivot */
  assert(pd-pa>=0);
  return pd-pa+1;     /* return number of suffixes equal to pivot */
}
Example #5
0
int deleteit( int L, int mm )
{
  int    nnl,k;
  double temp,t1;


  nnl = L - nc;
  if ( L != mr) 
   {    /* re-arrange data when specie  added or removed */
    dsw(wt,mr,L);
    dsw(w,mr,L);
    dsw(ff,mr,L);
    dsw(initial_chem_pot,mr,L);
    if ( si[mr] == SINGLE ) 
           dsw(fe,mr,L);
    swap2((int *)si,mr,L);
    dsw(fel,mr,L);
    swap2((int *)ic,nr,nnl);
    dsw(da,mr,L);       /* this seems unnecessary */
    swap2(ind,mr,L);
    rotate(index2, mr, L);  
    for ( k = 1; k <= nc; k++) 
       {
      t1 = sc[k][nr];
      sc[k][nr] = sc[k][nnl];
      sc[k][nnl] = t1;
    }
    for ( k = 1; k <= no_phases; k++) 
       {
      temp = d_mole[k][nr];
      d_mole[k][nr] = d_mole[k][nnl];
      d_mole[k][nnl] = temp;
    }
    for ( k = 1; k <= ne; k++) 
       {
      temp =bm[mr][k];
      bm[mr][k] = bm[L][k];
      bm[L][k] = temp;
    }
  }
  if (mm != 0) 
       return (TRUE);
  /* ------- extra procedures when removing a species --------------- */
  dsw(dg,nr,nnl);
  dsw(ds,mr,L);
  /* -------- special procedures for removing a phase --------------- */
  tmole[si[mr]] -= w[mr];
  nr--;                           /* one less reaction */
  mr--;                           /* one less reaction */
  if ( nr == 0 ) 
       return ( FALSE );
  else 
       return ( TRUE );
}
Example #6
0
int main(){
  int a,b,c,d,e;
  a = 1; b = 2; c = 3; d=4; e=5;
  printf("%d %d %d %d %d\n",a,b,c,d,e);
  /*bdeac*/
  swap2(&a,&b);
  swap2(&b,&d);
  swap2(&c,&e);
  /*hasta aquí*/
  printf("%d %d %d %d %d\n",a,b,c,d,e);
  return 0;
}
Example #7
0
static int median3(int *data, int left, int right) {
	int center = (left + right) / 2;
	if (data[left] > data[center]) {
		swap2(data, left, center);
	}
	if (data[left] > data[right]) {
		swap2(data, left, right);
	}
	if (data[center] > data[right]) {
		swap2(data, center, right);
	}
	// 三次比较后,data[left]<=data[center]<=data[right]
	swap2(data, center, right - 1);
	return data[right - 1];
}
Example #8
0
// 删除树根,最大值移到最后
static void deleteMax(int *data, int size) {
//	size--;
//	swap(&data[0], &data[size]);
//	percolateDown(data, 0, size);
	swap2(data, 0, size - 1); // 堆树根与最后一个data[size-1];交换
	percolateDown(data, 0, --size);
}
int main() {
        void swap1(int *p1, int *p2);
            void swap2(int *p1, int *p2);

                int a , b, c, d;
                    int *pointer_1, *pointer_2, *pointer_3, *pointer_4;

                        printf("Please enter a and b: ");

                            scanf("%d,%d", &a, &b);

                                printf("Please enter c and d: ");

                                    scanf("%d,%d", &c, &d);

                                        pointer_1 = &a;
                                            pointer_2 = &b;

                                                if (a<b) {
                                                            swap1(pointer_1, pointer_2);
                                                                }

                                                    pointer_3 = &c;
                                                        pointer_4 = &d;

                                                            if (c<d) {
                                                                        swap2(pointer_3, pointer_4);
                                                                            }

                                                                printf("max=%d, min=%d\n", a, b);
                                                                    printf("max=%d, min=%d\n", c, d);
                                                                        return 0;
}
Example #10
0
void cocktail() {

     uint i,

          j, 

          swapped;
     
     for(i = 0; i < n / 2; i++) {

         swapped = 0;

         for(j = i; j < n - 1 - i; j++) {

                 if( arr[ j ] > arr[j + 1] ) {

                     swap(j, j + 1);

                     swapped = 1;
                 }
         }

         for(j = n - 2 - i; j > i; j--) {

                 if( arr[ j ] < arr[j - 1] ) {

                     swap2(arr + j, arr + j - 1);

                     swapped = 1;
                 }
         }

         if( !swapped )  break;
     }
};
Example #11
0
int main()
{
    int i,j,k;
    int a[10]= { 4,5,8,1,2,9,10,3,6,7};
  
    for(i=0;i<10;i++)
    {
        for(k=0;k<9;k++)
        {
            if(a[k]>a[k+1])
            {
                //swap1(a[k],a[k+1]);
                swap2(&a[k],&a[k+1]);
            }
        }
            
    }

    for(j=0;j<10;j++)
    {
        printf("%d", a[j]);
    }
    printf("\n");
    return 0;
} 
Example #12
0
void sortColors3(int a[], int n) {
    int zero = 0;
    int two = n - 1;
    
    for (int i = 0; i <= two; i++) {
        if (a[i] == 0) {
            swap2(&a[zero], &a[i]);
            zero++;
        }
        if (a[i] == 2) {
            swap2(&a[two], &a[i]);
            two--;
            i--;
        }
    }
}
Example #13
0
main(){
    int x=1, y=2;
    printf("x=%d, y=%d\n", x, y);
    swap1(x,y);
    printf("After swap1(x,y): x=%d, y=%d\n", x, y);
    swap2(&x,&y);
    printf("After swap2(&x,&y): x=%d, y=%d\n", x, y);
}
Example #14
0
void ObjLoader::flipWinding(int4* faceInOut, int numTri)
{
	for(int i=0; i<numTri; i++)
	{
		int4& f = faceInOut[i];
		swap2( f.s[1], f.s[2] );
	}
}
Example #15
0
void ObjLoader::swapYZ(float4* vtxInOut, int numVtx)
{
	for(int i=0; i<numVtx; i++)
	{
		float4& v = vtxInOut[i];
		swap2( (float&)v.s[1], (float&)v.s[2] );
	}
}
Example #16
0
int main(){
	int a = 20;
	int b = 30;
	printf("a = %d,b = %d\n",a,b);
	swap2(&a,&b);
	printf("a = %d,b = %d\n",a,b);
	return 0;
}
Example #17
0
int partition(int *arr, int begin, int end){
	int pivot = arr[end - 1];
	int i, j, k;
	i = j = k = begin;

	for( ; k < end - 1; ++k){
		if(arr[k] < pivot){
			swap2(arr, i, j);
			++i;
		}

		++j;
	}

	swap2(arr, i, end - 1);
	return i;
}
Example #18
0
void ssort2(unsigned char **a, int n, int depth)
{   int d, r, partval;
    unsigned char **pa, **pb, **pc, **pd, **pl, **pm, **pn, *t;
    if (n < 10) {
        inssort(a, n, depth);
        return;
    }
    pl = a;
    pm = a + (n/2);
    pn = a + (n-1);
    if (n > 30) { 
        d = (n/8);
        pl = med3(pl, pl+d, pl+2*d);
        pm = med3(pm-d, pm, pm+d);
        pn = med3(pn-2*d, pn-d, pn);
    }
    pm = med3(pl, pm, pn);
    swap2(a, pm);
    partval = ptr2char(a);
    pa = pb = a + 1;
    pc = pd = a + n-1;
    for (;;) {
        while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) {
            if (r == 0) { swap2(pa, pb); pa++; }
            pb++;
        }
        while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) {
            if (r == 0) { swap2(pc, pd); pd--; }
            pc--;
        }
        if (pb > pc) break;
        swap2(pb, pc);
        pb++;
        pc--;
    }
    pn = a + n;
    r = min(pa-a, pb-pa);    vecswap2(a,  pb-r, r);
    r = min(pd-pc, pn-pd-1); vecswap2(pb, pn-r, r);
    if ((r = pb-pa) > 1)
        ssort2(a, r, depth);
    if (ptr2char(a + r) != 0)
        ssort2(a + r, pa-a + pn-pd-1, depth+1);
    if ((r = pd-pc) > 1)
        ssort2(a + n-r, r, depth);
}
Example #19
0
int main()
{
    unsigned long long x;
    int i, j;
    x = 0x00080004; i = 19, j = 0;
    printf("swap(%016llx, %d, %d) => %016llx\n", x, i, j, swap1(x, i, j));
    x = 0x00080004; i = 19, j = 0;
    printf("swap(%016llx, %d, %d) => %016llx\n", x, i, j, swap2(x, i, j));
}
Example #20
0
int main(int argc,char * crgv[])
{
	int a=1,b=1;
	printf("a=%d,b=%d\n",a,b);
	swap1(&a,&b);
	printf("a=%d,b=%d\n",a,b);
	swap2(&a,&b);
	printf("a=%d,b=%d\n",a,b);
}
Example #21
0
// 此方法来自DSAAC
static int partition3(int *data, int left, int right) {
	// 最后一个不需要移动
	// 因为pivotMedian返回时,最右边的数大于pivot
	// pivot位于 right-1的位置,因此j初始化为right-1
	int pivot = median3(data, left, right);
	int i = left;
	int j = right - 1;
	while (i < j) {
		while (data[++i] < pivot)
			;
		while (data[--j] > pivot)
			;
		if (i < j) {
			swap2(data, i, j);
		}
	}
	swap2(data, i, right - 1);
	return i;
}
Example #22
0
int main(int argc, char** argv) {
  double array[] = { 4, 6, 2};

  printf("Swapping values...\n");
  swap( array[0], array[2] );

  printf("Let's try new swapping ...\n");
  swap2( array+0, array+2 );

}
Example #23
0
void random(int data[], int size) {
	srand((unsigned) time(NULL ));
	for (int i = 0; i < size; i++) {
		data[i] = i;
	}
	for (int i = size - 1; i > 0; i--) {
		int j = rand() % (i + 1);
		swap2(data, i, j);
	}
}
Example #24
0
int main()
{
	int a=3,b=5;
	swap1(a,b);
	swap2(&a,&b);
	// swap3(a, b);

	printf("a:%d,b:%d\n", a, b);
	return 0;
}
int main() {
  int a = 12, b = 13;
  printf("Initial values:\n");
  printf("a = %d  b = %d\n\n", a, b);
  swap1(a, b);
  printf("After swap1:\n");
  printf("a = %d  b = %d\n\n", a, b);
  swap2(&a, &b);
  printf("After swap2:\n");
  printf("a = %d  b = %d\n", a, b);
}
Example #26
0
void inssort(unsigned char **a, int n, int d)
{   unsigned char **pi, **pj, *s, *t;
    for (pi = a + 1; --n > 0; pi++)
        for (pj = pi; pj > a; pj--) {
            for (s=*(pj-1)+d, t=*pj+d; *s==*t && *s!=0; s++, t++)
                ;
            if (*s <= *t)
                break;
            swap2(pj, pj-1);
    }
}
Example #27
0
static void insertsort(string *a, int n, int d)
{   string *pi, *pj, s, t;
    for (pi = a + 1; --n > 0; pi++)
        for (pj = pi; pj > a; pj--) {
            /* Inline strcmp: break if *(pj-1) <= *pj */
            for (s=*(pj-1)+d, t=*pj+d; *s==*t && *s!=0; s++, t++)
                ;
            if (*s <= *t)
                break;
            swap2(pj, pj-1);
    }
}
Example #28
0
int main() {
    int a[2] = {1, 2};
    swap(a, a+1);
    //@ assert a[0] == 2;
    //@ assert a[1] == 1;

    swap2(a, a+1);
    //@ assert a[0] == 1;
    //@ assert a[1] == 2;

    return 0;
}
Example #29
0
 //***************************************************************************
 //***************************************************************************
 int 
 ReadHTKFeature(
     FILE *    pInFp, 
     FLOAT *   pIn, 
     size_t    feaLen, 
     bool      swap,
     bool      decompress, 
     FLOAT *   pScale, 
     FLOAT *   pBias)
 {
   size_t i;
   
   if (decompress) 
   {
     INT_16 s;
 //    FLOAT pScale = (xmax - xmin) / (2*32767);
 //    FLOAT pBias  = (xmax + xmin) / 2;
 
     for (i = 0; i < feaLen; i++) 
     {
       if (fread(&s, sizeof(INT_16), 1, pInFp) != 1) 
         return -1;
       
       if (swap) swap2(s);
       pIn[i] = (s + pBias[i]) / pScale[i];
     }
     
     return 0;
   }
 
 #if !DOUBLEPRECISION
   if (fread(pIn, sizeof(FLOAT_32), feaLen, pInFp) != feaLen) 
     return -1;
   
   if (swap) 
     for (i = 0; i < feaLen; i++) 
       swap4(pIn[i]);
 #else
   FLOAT_32 f;
 
   for (i = 0; i < feaLen; i++) 
   {
     if (fread(&f, sizeof(FLOAT_32), 1, pInFp) != 1)
       return -1;
     
     if (swap) 
       swap4(f);
       
     pIn[i] = f;
   }
 #endif
   return 0;
 }  // int ReadHTKFeature
Example #30
0
int main(){
    double a = 5.5;
    double b = 10.5;
    print(a,b);
    swap1(a,b);
    print(a,b);
    swap2(a,b);   // Comment this line out
    print(a,b);   // Comment this line out
    swap3(&a,&b);
    print(a,b);    
    return 0;
}