Sorting a string aray in order of string length

Discussion in 'C Programming' started by Tom St Denis, Jul 9, 2003.

  1. Tom St Denis

    Tom St Denis Guest

    C.K. wrote:
    > Given an array of strings (i.e char *strings[10]), write C code to sort the
    > list in increasing order of string length. Run time efficiency is a primary
    > concern.
    >
    > Thanks a lot!


    Do your own homework?

    Tom
    Tom St Denis, Jul 9, 2003
    #1
    1. Advertising

  2. Tom St Denis

    C.K. Guest

    Given an array of strings (i.e char *strings[10]), write C code to sort the
    list in increasing order of string length. Run time efficiency is a primary
    concern.

    Thanks a lot!
    C.K., Jul 9, 2003
    #2
    1. Advertising

  3. Tom St Denis

    C.K. Guest

    No, I am now enjoying my summer vacation.

    This is a question that I find from 'C Challenge' of a website. I am just
    curious on how to solve it in an elegant way.

    Thanks in advance!


    "Tom St Denis" <> ?????
    news:_yUOa.103987$...
    > C.K. wrote:
    > > Given an array of strings (i.e char *strings[10]), write C code to sort

    the
    > > list in increasing order of string length. Run time efficiency is a

    primary
    > > concern.
    > >
    > > Thanks a lot!

    >
    > Do your own homework?
    >
    > Tom
    >
    C.K., Jul 9, 2003
    #3
  4. Tom St Denis

    pete Guest

    C.K. wrote:
    >
    > Given an array of strings (i.e char *strings[10]),
    > write C code to sort the list in increasing order of string length.
    > Run time efficiency is a primary concern.


    #include <string.h>
    #include <stdlib.h>

    int compare(const void *arg1, const void *arg2)
    {
    size_t L1, L2;

    L1 = strlen(arg1);
    L2 = strlen(arg2);
    return L2 > L1 ? -1 : L1 > L2;
    }

    qsort
    (strings, sizeof strings/sizeof *strings, sizeof *strings, compare);
    pete, Jul 9, 2003
    #4
  5. Tom St Denis

    Eric Sosman Guest

    "C.K." wrote:
    >
    > Given an array of strings (i.e char *strings[10]), write C code to sort the
    > list in increasing order of string length. Run time efficiency is a primary
    > concern.


    Why is run time efficiency a primary concern when sorting
    an array of just ten elements? With such a short array, how
    do you even hope to measure the run time to find out what kind
    of efficiency you've achieved?

    --
    Eric Sosman, Jul 9, 2003
    #5
  6. Tom St Denis

    Chris Dollin Guest

    Chris Dollin, Jul 9, 2003
    #6
  7. Tom St Denis

    Default User Guest

    "C.K." wrote:
    >
    > No, I am now enjoying my summer vacation.
    >
    > This is a question that I find from 'C Challenge' of a website. I am just
    > curious on how to solve it in an elegant way.



    Don't top-post. Your replies belong following properly trimmed quotes.

    The point of a C challenge is for YOU to work on it. Do your best and
    post it here for critique. We don't do programming for random people to
    admire.




    Brian Rodenborn
    Default User, Jul 9, 2003
    #7
  8. Tom St Denis

    Kevin Easton Guest

    pete <> wrote:
    > C.K. wrote:
    >>
    >> Given an array of strings (i.e char *strings[10]),
    >> write C code to sort the list in increasing order of string length.
    >> Run time efficiency is a primary concern.

    >
    > #include <string.h>
    > #include <stdlib.h>
    >
    > int compare(const void *arg1, const void *arg2)
    > {
    > size_t L1, L2;
    >
    > L1 = strlen(arg1);
    > L2 = strlen(arg2);
    > return L2 > L1 ? -1 : L1 > L2;
    > }
    >
    > qsort
    > (strings, sizeof strings/sizeof *strings, sizeof *strings, compare);


    You obviously didn't test this, because that won't work at all. The
    const void * arguments qsort passes to the compare function are pointers
    to the array elements - not the array elements themselves. In this
    case, they're pointer-to-pointer-to-char, converted to void *. In this
    case, the correct comparison function would be:

    int lencomp(const void *a, const void *b)
    {
    char * const *sa = a;
    char * const *sb = b;
    size_t alen = strlen(*sa);
    size_t blen = strlen(*sb);

    return (alen > blen) - (alen < blen);
    }

    - Kevin.
    Kevin Easton, Jul 10, 2003
    #8
  9. Tom St Denis

    pete Guest

    Kevin Easton wrote:
    >
    > pete <> wrote:
    > > C.K. wrote:
    > >>
    > >> Given an array of strings (i.e char *strings[10]),
    > >> write C code to sort the list in increasing order of string length.
    > >> Run time efficiency is a primary concern.

    > >
    > > #include <string.h>
    > > #include <stdlib.h>
    > >
    > > int compare(const void *arg1, const void *arg2)
    > > {
    > > size_t L1, L2;
    > >
    > > L1 = strlen(arg1);
    > > L2 = strlen(arg2);
    > > return L2 > L1 ? -1 : L1 > L2;
    > > }
    > >
    > > qsort
    > > (strings, sizeof strings/sizeof *strings, sizeof *strings, compare);

    >
    > You obviously didn't test this, because that won't work at all.
    > The const void * arguments qsort passes to the compare function
    > are pointers to the array elements
    > - not the array elements themselves.
    > In this case, they're pointer-to-pointer-to-char,
    > converted to void *.
    > In this case, the correct comparison function would be:
    >
    > int lencomp(const void *a, const void *b)
    > {
    > char * const *sa = a;
    > char * const *sb = b;
    > size_t alen = strlen(*sa);
    > size_t blen = strlen(*sb);
    >
    > return (alen > blen) - (alen < blen);
    > }


    Thank you.
    I make a mistake about that bad,
    almost every single time that I post untested code.

    My compiler likes it better this way:

    char const **sa = a;
    char const **sb = b;

    --
    pete
    pete, Jul 10, 2003
    #9
  10. Tom St Denis

    Kevin Easton Guest

    pete <> wrote:
    > Kevin Easton wrote:

    [...]
    >> int lencomp(const void *a, const void *b)
    >> {
    >> char * const *sa = a;
    >> char * const *sb = b;

    [...]
    > My compiler likes it better this way:
    >
    > char const **sa = a;
    > char const **sb = b;


    Then your compiler is broken :). const void *a; and char * const *sa;
    are equivalently-qualified pointers, but char const **sa; is not. The
    first points to a const-qualified void, the second to a const-qualified
    pointer to char, but the third points to a non-qualified pointer to a
    pointer to const-qualified char.

    - Kevin.
    Kevin Easton, Jul 10, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    3
    Views:
    14,091
    Karl Seguin
    Feb 17, 2005
  2. ss
    Replies:
    1
    Views:
    1,381
    Gordon Beaton
    Jun 22, 2006
  3. JoeC
    Replies:
    12
    Views:
    635
  4. max
    Replies:
    6
    Views:
    155
    RedGrittyBrick
    Jul 14, 2005
  5. Joza
    Replies:
    13
    Views:
    183
    -berlin.de
    Oct 3, 2006
Loading...

Share This Page