[Home]
[Contents]
[Chapter]
[Previous Algorithm]
[Next Algorithm]


Self-organizing (Transpose) sequential search (Pascal version available)


int search( key, r ) typekey key; dataarray r; { extern int n; int i; datarecord tempr; for ( i=0; i<n-1 && r[i].k != key; i++ ); if ( key == r[i].k ) { if ( i>0 ) { /*** Transpose with predecessor ***/ tempr = r[i]; r[i] = r[i-1]; r[--i] = tempr; }; return( i ); /*** found(r[i]) ***/ } else return( -1 ); /*** notfound(key) ***/ }

C source (313.srch.c) Pascal source (313.srch.p)



© Addison-Wesley Publishing Co. Inc.