Using qsort and bsearch, slightly adapted from http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_15.html