Thread: Sorting an array

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Sorting an array

    I have an array of strings that I need to sort alphabetically from a to z. What is the best way to do this? Could u please include a code sample for me to look at?

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    You can store all the letters into a character array and then use soemthing like bubblesort to sort them out using their ascii values, it's quite simple.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    That is what i want to do. The trouble is... I've had one class in C and that was over a year ago and I don't remember how to do that. I have the the words in the array. The array is called word[50][20]. I just need to know how to sort them. Thanks.

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Why have a double-subscripted array? Just do this:

    Code:
    char array[ 26 ] = { 'a', 'b', 'c' ........ };
    
    bubblesort( array ) {
       /* some other code */
       for ( i = 0; i < 26; i++ )
          for ( j = 0; j < 25; j++ )
             if ( array[ i ] > array[ i + 1 ] )
                swap them
    nice and easy, if you still don't understand i can give you teh full code when i have time.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Because when I read the array in, I have to limit the words to 20 characters or less. That is why I have the the second value.

  6. #6
    Unregistered
    Guest

    C makes it easy for you

    why don't you use qsort and strcmp, which should be given to you in the stdlib. Try this example, and i'm sure you'll be able to figure out how to use it. Read your compiler documentation for more info on qsort.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
        char names[4][10] = {"george", "jim", "mary", "bob"};
        int elements = 4, i;
    
        qsort (names, elements, 10, ( int (*) (const void*, const void*)) strcmp);
        for (i = 0; i < elements; ++i)
            printf ("%s\n", names[i]);
    
        return 0;
    }
    easy as that.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Ok here is what I'm trying to do:
    for(i=0;i<50;i++) //Sorts the contents of the array
    {
    // for(j=0;j<20;j++)
    //{
    if(strcmp(word[i+1],word[i])>0)
    {
    strcpy(datahold,word[i]);
    strcpy(word[i],word[i+1]);
    strcpy(word[i+1],datahold);
    }
    //}
    }
    Is there a way to make this work? This is the way I need to do it for the class. Plz help!!

  8. #8
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    first thing I would do is sit down with your professor because you don't really understand what you're trying to do. you do want a firm grasp on the material, right?

    If you can't you qsort, here's a easy bubble sort that will work for you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void b_sort_strings ( char * array[], int size)
    {
        char* temp;
        int i, j;
        for ( i = 0; i < size; ++i)
            for ( j = 0; j < size; ++j)
                if ( strcmp (array [i], array[j] < 0) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
    }

  9. #9
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I mean, why limit to 20 for the array? And if you're just comparing characters you don't need strcmp().

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And if you're just comparing characters you don't need strcmp().
    I agree, comparing strings with a function call is wasteful if you only need to sort by the first character in that string. In fact, even sorting the strings is wasteful since you can create a second array of size 20 containing pointers to the first element of each string.

    Sort the array of pointers by dereferencing them and comparing the characters pointed to. So instead of sorting arrays of 20 chars and being very clumsy about it, you sort little pointers. Then to read the sorted array, loop through the pointer array and dereference each string. They'll come out in sorted order.

    Piece of cake.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    To say what Prelude was saying in another way, you swap the addresses of your data, not the actuall data.

  12. #12
    Unregistered
    Guest
    WTF are you guys talking about? what if you have two strings "steve" and "stephen". Then what do you do?. you have to account for duplicate characters...

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Does'nt matter, i think he doesn't understand lol.

  14. #14
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    The unreigistered guy is right. That is what I have to do. I have to put 50 words in alphabetical order. So I have to account for duplicate characters in the words. That is why I have the 2nd dimension in the array.

  15. #15
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    http://www.google.com/search?hl=en&q=bubble+sort+C

    Gee, only 126,000 results, and that's just for bubble sort. Bubble sort's crap anyways - I've had professors who would fail me if they ever saw that in a program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM