Actual source code: sorti.c

  1: /*
  2:    This file contains routines for sorting integers. Values are sorted in place.


  5:    The word "register"  in this code is used to identify data that is not
  6:    aliased.  For some compilers, marking variables as register can improve 
  7:    the compiler optimizations.
  8:  */
 9:  #include petsc.h
 10:  #include petscsys.h

 12: #define SWAP(a,b,t) {t=a;a=b;b=t;}

 14: /* -----------------------------------------------------------------------*/

 18: /* 
 19:    A simple version of quicksort; taken from Kernighan and Ritchie, page 87.
 20:    Assumes 0 origin for v, number of elements = right+1 (right is index of
 21:    right-most member). 
 22: */
 23: static PetscErrorCode PetscSortInt_Private(PetscInt *v,PetscInt right)
 24: {
 26:   PetscInt i,vl,last,tmp;

 29:   if (right <= 1) {
 30:     if (right == 1) {
 31:       if (v[0] > v[1]) SWAP(v[0],v[1],tmp);
 32:     }
 33:     return(0);
 34:   }
 35:   SWAP(v[0],v[right/2],tmp);
 36:   vl   = v[0];
 37:   last = 0;
 38:   for (i=1; i<=right; i++) {
 39:     if (v[i] < vl) {last++; SWAP(v[last],v[i],tmp);}
 40:   }
 41:   SWAP(v[0],v[last],tmp);
 42:   PetscSortInt_Private(v,last-1);
 43:   PetscSortInt_Private(v+last+1,right-(last+1));
 44:   return(0);
 45: }

 49: /*@
 50:    PetscSortInt - Sorts an array of integers in place in increasing order.

 52:    Not Collective

 54:    Input Parameters:
 55: +  n  - number of values
 56: -  i  - array of integers

 58:    Level: intermediate

 60:    Concepts: sorting^ints

 62: .seealso: PetscSortReal(), PetscSortIntWithPermutation()
 63: @*/
 64: PetscErrorCode PetscSortInt(PetscInt n,PetscInt i[])
 65: {
 67:   PetscInt j,k,tmp,ik;

 70:   if (n<8) {
 71:     for (k=0; k<n; k++) {
 72:       ik = i[k];
 73:       for (j=k+1; j<n; j++) {
 74:         if (ik > i[j]) {
 75:           SWAP(i[k],i[j],tmp);
 76:           ik = i[k];
 77:         }
 78:       }
 79:     }
 80:   } else {
 81:     PetscSortInt_Private(i,n-1);
 82:   }
 83:   return(0);
 84: }

 86: #define SWAP2(a,b,c,d,t) {t=a;a=b;b=t;t=c;c=d;d=t;}

 88: /* -----------------------------------------------------------------------*/

 92: /* 
 93:    A simple version of quicksort; taken from Kernighan and Ritchie, page 87.
 94:    Assumes 0 origin for v, number of elements = right+1 (right is index of
 95:    right-most member). 
 96: */
 97: static PetscErrorCode PetscSortIntWithArray_Private(PetscInt *v,PetscInt *V,PetscInt right)
 98: {
100:   PetscInt i,vl,last,tmp;

103:   if (right <= 1) {
104:     if (right == 1) {
105:       if (v[0] > v[1]) SWAP2(v[0],v[1],V[0],V[1],tmp);
106:     }
107:     return(0);
108:   }
109:   SWAP2(v[0],v[right/2],V[0],V[right/2],tmp);
110:   vl   = v[0];
111:   last = 0;
112:   for (i=1; i<=right; i++) {
113:     if (v[i] < vl) {last++; SWAP2(v[last],v[i],V[last],V[i],tmp);}
114:   }
115:   SWAP2(v[0],v[last],V[0],V[last],tmp);
116:   PetscSortIntWithArray_Private(v,V,last-1);
117:   PetscSortIntWithArray_Private(v+last+1,V+last+1,right-(last+1));
118:   return(0);
119: }

123: /*@
124:    PetscSortIntWithArray - Sorts an array of integers in place in increasing order;
125:        changes a second array to match the sorted first array.

127:    Not Collective

129:    Input Parameters:
130: +  n  - number of values
131: .  i  - array of integers
132: -  I - second array of integers

134:    Level: intermediate

136:    Concepts: sorting^ints with array

138: .seealso: PetscSortReal(), PetscSortIntPermutation(), PetscSortInt()
139: @*/
140: PetscErrorCode PetscSortIntWithArray(PetscInt n,PetscInt i[],PetscInt I[])
141: {
143:   PetscInt j,k,tmp,ik;

146:   if (n<8) {
147:     for (k=0; k<n; k++) {
148:       ik = i[k];
149:       for (j=k+1; j<n; j++) {
150:         if (ik > i[j]) {
151:           SWAP2(i[k],i[j],I[k],I[j],tmp);
152:           ik = i[k];
153:         }
154:       }
155:     }
156:   } else {
157:     PetscSortIntWithArray_Private(i,I,n-1);
158:   }
159:   return(0);
160: }