home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!paladin.american.edu!darwin.sura.net!news.duc.auburn.edu!ducvax.auburn.edu!swanger
- From: swanger@ducvax.auburn.edu
- Subject: Sort help needed
- Message-ID: <1992Aug12.154130.1@ducvax.auburn.edu>
- Lines: 45
- Sender: usenet@news.duc.auburn.edu (News Account)
- Nntp-Posting-Host: ducvax
- Organization: Auburn University, AL
- Date: Wed, 12 Aug 1992 20:41:30 GMT
- Lines: 45
-
- I am writing a program in C. I have an array of character strings that
- I want to sort (I dynamically allocate the "array" with a series of
- calloc statements, so to the purist I suppose it isn't really an array,
- I just pretend like it is one). The array usually contains around 1800
- strings of 512 characters each. I sort the array (sort the pointers
- actually) with the following sort routine (on a SUN IPC Sparcstation)
- and it usually takes 8 to 10 seconds to finish. Maybe I am too spoiled
- or impatient or something, but I really wish that I could speed this
- sort up. If any of you have any tips, functions, ideas, etc. regarding
- sorting, I would really appreciate hearing them!
-
- Maybe I should add that many of the character strings only contain a
- few characters and are padded with blanks to the "right". Since each
- string is 512 characters, would the sort speed up if I realloced each
- string to it's shortest length? Some of the strings are really long,
- so I originally set the default line length for all of the strings to
- 512, but many of them don't have to be this long.
-
- Thanks in advance,
-
-
- ------------------------------------------------------------------------
-
- void sort_strings(strings, num)
- char *strings[];
- int num;
- {
- char *temp;
- int top, seek;
-
- for (top = 0; top < num-1; top++)
- {
- for (seek = top + 1; seek < num; seek++)
- {
- if (strcmp(strings[top],strings[seek]) > 0)
- {
- temp = strings[top];
- strings[top] = strings[seek];
- strings[seek] = temp;
- }
- }
- }
- }
- --
- David Swanger, Auburn University, Alabama, swanger@ducvax.auburn.edu
-