home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12298 < prev    next >
Encoding:
Text File  |  1992-08-13  |  1.9 KB  |  50 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gumby!destroyer!gatech!hubcap!mjs
  3. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  4. Subject: Re: sort routines
  5. Message-ID: <1992Aug13.201157.25506@hubcap.clemson.edu>
  6. Organization: Clemson University, Clemson SC
  7. References: <1992Aug13.170424@axion.bt.co.uk>
  8. Date: Thu, 13 Aug 1992 20:11:57 GMT
  9. Lines: 39
  10.  
  11. In article <1992Aug13.170424@axion.bt.co.uk> pkeddie@axion.bt.co.uk (Paul Keddie) writes:
  12. >
  13. >How do people sort a single linked list in a C program ? I know there
  14. >is thw qsort routine but that seems to assume you know the maximum possible
  15. >size of the list. 
  16. >
  17. >Do you just have to resort to writing your own routine, and if so which is the
  18. >best for a single linked list data structure (insertion,bubble.etc).
  19.  
  20. If you want to actually sort the linked structure, you need to write a
  21. merge or bucket sort to do it (insertion sort would work, but is
  22. probably slower--insertion sort is what you would do if you built the
  23. list in sorted order to begin with).
  24.  
  25. On the other hand, what about creating an array of pointers to
  26. elements of the list, and running qsort() on it, then rebuilding the
  27. list?  Yes, you need to know how many elements are in the list, but
  28. only at the moment you do the sort.  You can malloc() the space for
  29. the array at that point.
  30.  
  31. If its a big list, or you are sorting it frequently or searching it
  32. frequently, you might think about using some other data structure
  33. (like a tree), or using a heuristic to cut down on access times in an
  34. unsorted list (such as moving the most recently accessed element to
  35. the front), depending on the characteristics of the list access
  36. requests.
  37.  
  38. >Cheers Paul
  39. >Software Maintenance Group, Software Technology Division, BT
  40. >Research Labs, Martlesham Heath, Ipswich, IP5 7RE, UK
  41. >E-mail:  pkeddie@axion.bt.co.uk
  42. >Phone:  +44 473 649154
  43. >Fax:    +44 473 643019 
  44.  
  45.  
  46. -- 
  47.         Matthew Saltzman
  48.         Clemson University Math Sciences
  49.         mjs@clemson.edu
  50.