Actual source code: sorti.c

  1: /*$Id: sorti.c,v 1.30 2001/08/06 21:14:16 bsmith Exp $*/
  2: /*
  3:    This file contains routines for sorting integers. Values are sorted in place.


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

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

 15: /* -----------------------------------------------------------------------*/

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

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

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

 48:    Not Collective

 50:    Input Parameters:
 51: +  n  - number of values
 52: -  i  - array of integers

 54:    Level: intermediate

 56:    Concepts: sorting^ints

 58: .seealso: PetscSortReal(), PetscSortIntWithPermutation()
 59: @*/
 60: int PetscSortInt(int n,int i[])
 61: {
 62:   int ierr,j,k,tmp,ik;

 65:   if (n<8) {
 66:     for (k=0; k<n; k++) {
 67:       ik = i[k];
 68:       for (j=k+1; j<n; j++) {
 69:         if (ik > i[j]) {
 70:           SWAP(i[k],i[j],tmp);
 71:           ik = i[k];
 72:         }
 73:       }
 74:     }
 75:   } else {
 76:     PetscSortInt_Private(i,n-1);
 77:   }
 78:   return(0);
 79: }

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

 83: /* -----------------------------------------------------------------------*/

 85: /* 
 86:    A simple version of quicksort; taken from Kernighan and Ritchie, page 87.
 87:    Assumes 0 origin for v, number of elements = right+1 (right is index of
 88:    right-most member). 
 89: */
 90: static int PetscSortIntWithArray_Private(int *v,int *V,int right)
 91: {
 92:   int i,vl,last,tmp,ierr;

 95:   if (right <= 1) {
 96:     if (right == 1) {
 97:       if (v[0] > v[1]) SWAP2(v[0],v[1],V[0],V[1],tmp);
 98:     }
 99:     return(0);
100:   }
101:   SWAP2(v[0],v[right/2],V[0],V[right/2],tmp);
102:   vl   = v[0];
103:   last = 0;
104:   for (i=1; i<=right; i++) {
105:     if (v[i] < vl) {last++; SWAP2(v[last],v[i],V[last],V[i],tmp);}
106:   }
107:   SWAP2(v[0],v[last],V[0],V[last],tmp);
108:   PetscSortIntWithArray_Private(v,V,last-1);
109:   PetscSortIntWithArray_Private(v+last+1,V+last+1,right-(last+1));
110:   return(0);
111: }

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

117:    Not Collective

119:    Input Parameters:
120: +  n  - number of values
121: .  i  - array of integers
122: -  I - second array if integers

124:    Level: intermediate

126:    Concepts: sorting^ints with array

128: .seealso: PetscSortReal(), PetscSortIntPermutation(), PetscSortInt()
129: @*/
130: int PetscSortIntWithArray(int n,int i[],int I[])
131: {
132:   int ierr,j,k,tmp,ik;

135:   if (n<8) {
136:     for (k=0; k<n; k++) {
137:       ik = i[k];
138:       for (j=k+1; j<n; j++) {
139:         if (ik > i[j]) {
140:           SWAP2(i[k],i[j],I[k],I[j],tmp);
141:           ik = i[k];
142:         }
143:       }
144:     }
145:   } else {
146:     PetscSortIntWithArray_Private(i,I,n-1);
147:   }
148:   return(0);
149: }