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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!paladin.american.edu!darwin.sura.net!news.duc.auburn.edu!ducvax.auburn.edu!swanger
  3. From: swanger@ducvax.auburn.edu
  4. Subject: Sort help needed
  5. Message-ID: <1992Aug12.154130.1@ducvax.auburn.edu>
  6. Lines: 45
  7. Sender: usenet@news.duc.auburn.edu (News Account)
  8. Nntp-Posting-Host: ducvax
  9. Organization: Auburn University, AL
  10. Date: Wed, 12 Aug 1992 20:41:30 GMT
  11. Lines: 45
  12.  
  13. I am writing a program in C.  I have an array of character strings that
  14. I want to sort (I dynamically allocate the "array" with a series of
  15. calloc statements, so to the purist I suppose it isn't really an array,
  16. I just pretend like it is one).  The array usually contains around 1800
  17. strings of 512 characters each.  I sort the array (sort the pointers
  18. actually) with the following sort routine (on a SUN IPC Sparcstation)
  19. and it usually takes 8 to 10 seconds to finish.  Maybe I am too spoiled
  20. or impatient or something, but I really wish that I could speed this
  21. sort up.  If any of you have any tips, functions, ideas, etc. regarding
  22. sorting, I would really appreciate hearing them!
  23.  
  24. Maybe I should add that many of the character strings only contain a
  25. few characters and are padded with blanks to the "right".  Since each
  26. string is 512 characters, would the sort speed up if I realloced each
  27. string to it's shortest length?  Some of the strings are really long,
  28. so I originally set the default line length for all of the strings to
  29. 512, but many of them don't have to be this long.
  30.  
  31. Thanks in advance,
  32.  
  33.  
  34. ------------------------------------------------------------------------
  35.  
  36. void sort_strings(strings, num)
  37. char *strings[];
  38. int num;
  39. {
  40.    char *temp;
  41.    int top, seek;
  42.  
  43.    for (top = 0; top < num-1; top++)
  44.    {
  45.       for (seek = top + 1; seek < num; seek++)
  46.       {
  47.          if (strcmp(strings[top],strings[seek]) > 0)
  48.          {
  49.             temp = strings[top];
  50.             strings[top] = strings[seek];
  51.             strings[seek] = temp;
  52.          }
  53.       }
  54.    }
  55. }
  56. -- 
  57. David Swanger,   Auburn University, Alabama,   swanger@ducvax.auburn.edu 
  58.