Thread: sorting alphabets...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    sorting alphabets...

    Hey guys:

    I'm having a problem figuring out a part of my newest assignment for class. I'm suppose to use structures to create a database phone number list with first and last name.

    The problem I'm having to figure out is trying to sort and print from the first name or from the last name. Could some give me an example of an algorithm? For the first name, I would only have to look at the first element (first letter of the name), and then compare it with the other phone list entries. But how would I keep track and put them in order from A-Z?

    Can anyone help?

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    will this work? All I did was basically manipulate a bubble sort (or maybe it's a selection sort) algorithm that I used before in another program to fit for strings/arrays...Will it work?

    Code:
    #define M 10     
    #define SIZE 10
    struct info
    {
      char name[SIZE+1];
      char surname[SIZE+1];
      char phone[12+1];
    };
    
    struct info entry[M] = {0};
    Code:
    char temp[SIZE];
    
              if(sort_input == 'n')      //NAME SORT                                                                                                       
                {
                  for(x=0; x<M; x++)
                    {
                      for(y=0; y<M-1; y++)
                        {
                          if(entry[y].name[0] > entry[y+1].name[0])
                            {
                              temp = entry[y+1].name;
                              entry[y+1].name = entry[y].name;
                              entry[y].name = temp;
                            }
                        }
    		}
                }
    This is a snippit of my code...

    Any suggestions?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Why not try it out and see?

    I didn't read the code, but the idea should work.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    because I haven't finished the code entirely, I just wanted to see what everyone thought before I went on...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Instead of "stupid sort", perhaps you can use insertion sort, so that each element is inserted at the position it belongs in the list.

    By the way, your sort isn't going to work. You need to compare the WHOLE name, and you need to use strcpy() to copy the strings when you swap the elements.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a selection sort of an array of structs called folders, which has just two fields in it: a name field = nfield (type char and 75 char's long)

    and a

    points field = pfield. (unsigned long int type)

    Where k -1 = the size of the array to be sorted.

    In my program, this function is called repeatedly to sort small subsets of the whole array, which vary in size. It's blazing fast, but don't use it for 10's of thousands of records. That's for Qsort, and other top-notch sorting algorithm's.


    Code:
    void sortMilestoners(int k)  {
       int i, j;
       char *name = malloc(75);
       unsigned long int points;
    
       points = 0; name = '\0';
       for(i = 0; i < k-1; i++)  {
          for(j = i+1; j < k; j++)  {
             if(strcmp(folders[i].nfield, folders[j].nfield) > 0)  {
                name = '\0';
                strcpy(name, folders[i].nfield);
                folders[i].nfield[0] = '\0';
                strcpy(folders[i].nfield, folders[j].nfield);
                folders[j].nfield[0] = '\0';
                strcpy(folders[j].nfield, name);
    
                points = folders[i].pfield;
                folders[i].pfield = folders[j].pfield;
                folders[j].pfield = points;
             }
          }
       }
       putchar('\n');
       for(i = 0; i < k; i++)  {
          fprintf(out, "\n%s, %lu", folders[i].nfield, folders[i].pfield);
          /* for debug help */
    //      printf("\n%s, %lu", folders[i].nfield, folders[i].pfield);
    //    if(i % 20 == 0)
    //       sleep(5);
       }
       putchar('\n');
       fprintf(out, "\n");
       free(name);
    }

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    Adak:

    Code:
    for(i = 0; i < k-1; i++)  {
          for(j = i+1; j < k; j++)  {
             if(strcmp(folders[i].nfield, folders[j].nfield) > 0)  {
                name = '\0';
                strcpy(name, folders[i].nfield);
                folders[i].nfield[0] = '\0';
                strcpy(folders[i].nfield, folders[j].nfield);
                folders[j].nfield[0] = '\0';
                strcpy(folders[j].nfield, name);
    
                points = folders[i].pfield;
                folders[i].pfield = folders[j].pfield;
                folders[j].pfield = points;
             }
          }
       }
    So if I am reading your code correctly, you are basically comparing a two strings via strcmp, then using a temp variable called name to store a string in there, then basically using strcpy to swap the items?

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    also, is

    Code:
    folders[i].nfield[0] = '\0';
    is the same thing as this?

    Code:
    folders[i].nfield[0] = 0;
    ??

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dcwang3 View Post
    also, is

    Code:
    folders[i].nfield[0] = '\0';
    is the same thing as this?

    Code:
    folders[i].nfield[0] = 0;
    ??
    yes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    ok so here is part of the code I am working on, it's sort of a manipulation of the code from above:

    Code:
    if(sort_input == 'n')      //NAME SORT                                                                                                        
                {
                  for(i = 0; i < (M-1); i++)
                    {
    		  for(j = (i+1); j < M; j++)
                      {
                      if(strcmp(entry[i].name, entry[j].name) > 0)
                        {
                          for(i=0; i< SIZE+1; i++)
                            {
                              name_temp[i] = 0;
                            }
    		      strcpy(name_temp, entry[i].name);
                          printf("&#37;s", name_temp);
                          // entry[i].name[0] = '\0';                                                                                                       
    	              // strcpy(entry[i].name, entry[j].name);                                                                                          
                          // entry[j].name[0] = '\0';                                                                                                       
                          // strcpy(entry[j].name, name_temp);                                                                                              
    
                        }
                      }
                    }
    
                }
    The problem is when it goes through this part, I'm getting an output for the printf of name_temp (this is for debugging purposes...) to be ??????? . For some reason it seems like the strcpy isn't working.

    For the entries that are being compared, I put in David, then I put in Alvin, so that David would be in index 0, and Alvin would be index 1. So if I understand it correctly, David > Alvin thus it should process through the statement within the IF statements?

    Can someone help?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dcwang3 View Post
    ok so here is part of the code I am working on, it's sort of a manipulation of the code from above:

    Code:
    if(sort_input == 'n')      //NAME SORT                                                                                                        
                {
                  for(i = 0; i < (M-1); i++)
                    {
    		  for(j = (i+1); j < M; j++)
                      {
                      if(strcmp(entry[i].name, entry[j].name) > 0)
                        {
                          for(i=0; i< SIZE+1; i++)
                            {
                              name_temp[i] = 0;
                            }
    		      strcpy(name_temp, entry[i].name);
                          printf("%s", name_temp);
                          // entry[i].name[0] = '\0';                                                                                                       
    	              // strcpy(entry[i].name, entry[j].name);                                                                                          
                          // entry[j].name[0] = '\0';                                                                                                       
                          // strcpy(entry[j].name, name_temp);                                                                                              
    
                        }
                      }
                    }
    
                }
    The problem is when it goes through this part, I'm getting an output for the printf of name_temp (this is for debugging purposes...) to be ??????? . For some reason it seems like the strcpy isn't working.

    For the entries that are being compared, I put in David, then I put in Alvin, so that David would be in index 0, and Alvin would be index 1. So if I understand it correctly, David > Alvin thus it should process through the statement within the IF statements?

    Can someone help?
    That for loop using i blew away your original value of i.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    when you have an array like this:

    entry[i].name

    Can you automatically assume that it is a pointer and you can assign it to 0 or \0 like this:

    entry[i].name = 0 or entry[i].name = '\0'

    I was trying to do that in my code, but I was getting a warning saying like assignment type is not alike (not exactly that, but something like that), so I decided to assign the first entry. But if I only assign the first entry entry[i].name[0], that does not make all the entries 0 in name (like when you initialize it at the beginning)?

    Is there any other way than using a for loop to make the entire array equal to zero?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C actually converts ALL array references to pointers and offsets, immediately. The index numbers, are just there to help us humans.

    entry[i].name[0] = '\0; is what I use, and yes, it only sets the first char in the name string to the end of string char, but that's all you need. No need to set the entire name string to '\0'.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    Code:
    if(sort_input == 'n')      //NAME SORT                                                                                                                     
                {
                  for(i = 0; i < (M-1); i++)
                    {
                      for(j = (i+1); j < M; j++)
                        {
                          if(strcmp(entry[i].name, entry[j].name) > 0 && (entry[j].name != 0))
                            {
                              for(x=0; x< SIZE+1; x++)
                                {
                                  name_temp[x] = '\0';
                                }
                              strcpy(name_temp, entry[i].name);
                              entry[i].name[0] = '\0';
                              strcpy(entry[i].name, entry[j].name);
                              entry[j].name[0] = '\0';
                              strcpy(entry[j].name, name_temp);
                            }
    
                        }
                    }
    
                }
    So I changed the for loop inside so that the it does not conflict with the first i variable. But when I run the program, and it goes through this part of the code, for the output, it doesn't show up anything, like it's blank entries when it prints out. The examples I'm using is David for the first entry, then Alvin for the second entry. It seems like it keep comparing all of the entries rather than stopping after entry[j].name is = '\0'.

    Does anyone else believe that this is happening?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't tell what you're doing, because you don't have all your code posted, but remember that you don't sort EVERY array element.

    You only sort the array elements that have a valid string in them. That should be what M is giving to this block of code.

    That's what int k did for my sorting function, also.

    When you put your strings into the array, you count up as you go, how many you're putting into the array. That's if you want to use two for loops in your sorting function (which I like using) for a simple selection sort.

    If you want more help, you need to show more of your code (or describe ALL the variables PRECISELY, and also SHOW THE INPUT AND OUTPUT you're getting.

    Your descriptions are just not exact enough, without that. What we may or may not believe, is irrelevant. This is programming, not Religion.

    Well, sometimes programming is a religion, but .... oh, nevermind!
    Last edited by Adak; 11-27-2008 at 03:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM