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


Quicksort (with bounded stack usage) (Pascal version available)


sort( r, lo, up ) ArrayToSort r; int lo, up; {int i, j; ArrayEntry tempr; while ( up>lo ) { i = lo; j = up; tempr = r[lo]; /*** Split file in two ***/ while ( i<j ) { for ( ; r[j].k > tempr.k; j-- ); for ( r[i]=r[j]; i<j && r[i].k<=tempr.k; i++ ); r[j] = r[i]; } r[i] = tempr; /*** Sort recursively, the smallest first ***/ if ( i-lo < up-i ) { sort(r,lo,i-1); lo = i+1; } else { sort(r,i+1,up); up = i-1; } } }

C source (413.sort.c) Pascal source (413.sort.p)



© Addison-Wesley Publishing Co. Inc.