Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

cs_sort_ff.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 #include "cs.h"
00008 
00009 /********************************************************************************/
00010 /* insertion_sort : sort an array of float + float                              */
00011 
00012 static void isort_floatfloat( int n , float * ar , float * iar )
00013 {
00014    register int  j , p ;  /* array indices */
00015    register float temp ;  /* a[j] holding place */
00016    register float  itemp ;
00017    register float * a = ar ;
00018    register float * ia = iar ;
00019 
00020    if( n < 2 ) return ;
00021 
00022    for( j=1 ; j < n ; j++ ){
00023 
00024      if( a[j] < a[j-1] ){   /* out of order */
00025         p    = j ;
00026         temp = a[j] ; itemp = ia[j] ;
00027 
00028        do{
00029            a[p] =  a[p-1] ; /* at this point, a[p-1] > temp, so move it up */
00030           ia[p] = ia[p-1] ;
00031           p-- ;
00032         } while( p > 0 && temp < a[p-1] ) ;
00033 
00034         a[p] = temp ;       /* finally, put temp in its place */
00035        ia[p] = itemp ;
00036      }
00037    }
00038 }
00039 
00040 /********************************************************************************/
00041 /* qsrec : recursive part of quicksort (stack implementation)                   */
00042 
00043 #define QS_STACK  1024  /* stack size */
00044 #define QS_SWAPF(x,y) ( temp=(x),(x)=(y),(y)= temp)
00045 #define QS_SWAPI(i,j) (itemp=(i),(i)=(j),(j)=itemp)
00046 
00047 static void qsrec_floatfloat( int n , float * ar , float * iar , int cutoff )
00048 {
00049    register int i , j ;           /* scanning indices */
00050    register float temp , pivot ;  /* holding places */
00051    register float ipivot ;
00052    register float * a = ar ;
00053    register float * ia = iar ;
00054    int itemp ;
00055 
00056    int left , right , mst , stack[QS_STACK] , nnew ;
00057 
00058    /* return if too short (insertion sort will clean up) */
00059 
00060    if( cutoff < 3 ) cutoff = 3 ;
00061    if( n < cutoff ) return ;
00062 
00063    /* initialize stack to start with whole array */
00064 
00065    stack[0] = 0   ;
00066    stack[1] = n-1 ;
00067    mst      = 2   ;
00068 
00069    /* loop while the stack is nonempty */
00070 
00071    while( mst > 0 ){
00072       right = stack[--mst] ;  /* work on subarray from left -> right */
00073       left  = stack[--mst] ;
00074 
00075       i = ( left + right ) / 2 ;           /* middle of subarray */
00076 
00077       /* sort the left, middle, and right a[]'s */
00078 
00079       if( a[left] > a[i]     ){ QS_SWAPF(a[left] ,a[i]    ); QS_SWAPF(ia[left] ,ia[i]    ); }
00080       if( a[left] > a[right] ){ QS_SWAPF(a[left] ,a[right]); QS_SWAPF(ia[left] ,ia[right]); }
00081       if( a[i] > a[right]    ){ QS_SWAPF(a[right],a[i]    ); QS_SWAPF(ia[right],ia[i]    ); }
00082 
00083       pivot  = a[i] ;                        /* a[i] is the median-of-3 pivot! */
00084       a[i]   = a[right] ;
00085       ipivot = ia[i] ;
00086       ia[i]  = ia[right] ;
00087 
00088       i = left ;                            /* initialize scanning */
00089       j = right ;
00090 
00091       /*----- partition:  move elements bigger than pivot up and elements
00092                           smaller than pivot down, scanning in from ends -----*/
00093 
00094       do{
00095         for( ; a[++i] < pivot ; ) ;  /* scan i up,   until a[i] >= pivot */
00096         for( ; a[--j] > pivot ; ) ;  /* scan j down, until a[j] <= pivot */
00097 
00098         if( j <= i ) break ;         /* if j meets i, quit */
00099 
00100         QS_SWAPF( a[i] , a[j] ) ; QS_SWAPF( ia[i] , ia[j] ) ;
00101       } while( 1 ) ;
00102 
00103       /*----- at this point, the array is partitioned -----*/
00104 
00105       a[right]  = a[i] ;           /*restore the pivot*/
00106       a[i]      = pivot ;
00107       ia[right] = ia[i] ;
00108       ia[i]     = ipivot ;
00109 
00110       /*----- push subarrays [left..i-1] and [i+1..right] onto stack, if big -----*/
00111 
00112       nnew = 0 ;
00113       if( (i-left)  > cutoff ){ stack[mst++] = left ; stack[mst++] = i-1   ; nnew++ ; }
00114       if( (right-i) > cutoff ){ stack[mst++] = i+1  ; stack[mst++] = right ; nnew++ ; }
00115 
00116       /* if just added two subarrays to stack, make sure shorter one comes first */
00117 
00118       if( nnew == 2 && stack[mst-3] - stack[mst-4] > stack[mst-1] - stack[mst-2] ){
00119          QS_SWAPI( stack[mst-4] , stack[mst-2] ) ;
00120          QS_SWAPI( stack[mst-3] , stack[mst-1] ) ;
00121       }
00122 
00123    }  /* end of while stack is non-empty */
00124 
00125 }
00126 
00127 /********************************************************************************/
00128 /* quick_sort :  sort an array partially recursively, and partially insertion   */
00129 
00130 #ifndef QS_CUTOFF
00131 #define QS_CUTOFF 10
00132 #endif
00133 
00134 void qsort_floatfloat( int n , float * a , float * ia )
00135 {
00136    qsrec_floatfloat( n , a , ia , QS_CUTOFF ) ;
00137    isort_floatfloat( n , a , ia ) ;
00138    return ;
00139 }
 

Powered by Plone

This site conforms to the following standards: