bubble sorting multiple strings

This is a discussion on bubble sorting multiple strings within the C Programming forums, part of the General Programming Boards category; i have 100 records which need to be sorted by the customers first name. i am able to write bubble ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    bubble sorting multiple strings

    i have 100 records which need to be sorted by the customers first name. i am able to write bubble sorts for integers but not for strings.
    i can't get my head around converting a string to an int.

    for example the names may be stored in a text file like this

    Ross
    Rachel
    Phobe
    Joey
    Chandler
    Monica

    and the desired output is

    Chandler
    Joey
    Monica
    Phobe
    Rachel
    Ross

    now as i said, i can do a bubble sort with numbers so if i could for example, convert

    Chandler to 3, 8, 1, 14, 4, 12, 5, 18 (thats changing each letter to its corresponding number) i could sort it, but i can't figure out how to do that. alternitivly there could be another way of sorting these directly, but i cannot find the answer anywhere.

    it must be bubble sort as its for an assignment.

    thank you very much for your time.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Definition of problem:
    1) How to sort strings

    Use strcmp() in place of comparing each integer in the sort you already know.
    Use strcpy() to copy the strings during the switch.

    2) How to sort data that is in multiple arrays (like name, address, age, etc.) and not in a structure:

    Define one more integer array, say list loading the values 0 to n (size of your other arrays)

    Then reference the name array using list:
    name[list[i]] instead of simply name[i]
    when two names need to be exchanged, simply exchange the list[] values.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    thanks, after reading further into my assignment, i realise that i will need to convert the strings into arrays of integers. i suppose this can be done with an if getchr type thing? but any extra tips would be great!

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Convert into strings of integers? Your strings are already arrays of integers. Characters are stored as integers (ascii codes), right?

    Also, you might consider using tolower() or toupper() in your sorting function (or simply write your own logic to identify ascii codes of uppercase letters and convert them to their lowercase equivalents) to make the sort purely alphabetical and not case-sensitive. Otherwise, for example, BERRY comes before Banana.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    oh, i suppose that makes sense about them already being integers i don't fully understand it. this is the code for my function, i copied the code from a book and changed it to how i thought that it should be. i got 3 errors (but i havent set up the rest of my program to run this function yet, the errors were all within this function and they were all: error C2106: '=' : left operand must be l-value. the errors are on the first 3 lines of the if statement. i don't know what that means or how to fix it.

    Code:
    void bubbleSort(struct customers cust[], int numCust)
    
    {
    	int i, j, moves = 0, noSwaps, checks;
    	char temp[21];
    	noSwaps = FALSE;
    	checks = numCust;
    	
    	while (noSwaps == FALSE && checks > 0)
    	{
    		noSwaps = TRUE;
    		for(j = 1; j < numCust; j++)
    		{
    			if (cust[j].recName < cust[j-1].recName)
    			{
    				temp = cust[j].recName;
    				cust[j].recName = cust[j-1].recName;
    				cust[j-1].recName = temp;
    				noSwaps = FALSE;
    				moves++;
    			}
    		}
    		checks--;
    	}
    }
    but back to the whole the string is already an array of integers that helps me i suppose but i can't just change char to int[] can i?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Hi, jibbles.

    The problem is that you cannot assign one array to another using '='. Keep in mind that the array's name is actually the first element's address!

    Use

    Code:
    strcpy(temp, cust[j].recName);
    instead of

    Code:
    temp = cust[j].recName;
    as WaltP already mentioned.

    Regards,
    Ralf

    By the way: I recommend to get Kernighan and Ritchie's "The C-programming-language". This book is your friend! Especially when it comes to basic problems with pointers and arrays. I am programming for over 10 years now and I still don't want to miss it. I am quite sure that every experienced C-Programmer will agree.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    thanks heaps for your help guys i really appreciate it.
    i'm trying so hard to get this done and there is still so much more to do but it's just not working! i've got it down to no errors but its not actually sorting it, or if it is, it's not showing me that it is.

    Code:
    	while (noSwaps == FALSE && checks > 0)
    	{
    		noSwaps = TRUE;
    		for(j = 1; j < numCust; j++)
    		{
    			if (cust[j].recName < cust[j-1].recName)
    			{
    				strcpy(temp, cust[j].recName);
    				strcpy(cust[j-1].recName, cust[j].recName);
    				strcpy(temp, cust[j-1].recName);
    				noSwaps = FALSE;
    				moves++;
    			}
    		}
    		checks--;
    	}
    i think the problem must be in that somewhere or that maybe i have to somehow save the changes to the text file that the customers records are saved in?

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    As rpet said, you can't use = to assign contents of one array to another. Either use strcpy(), or if you're not supposed to be using string functions yet, you can write your own loop to copy one element at a time.

    But is that really what you want to do? Maybe I'm misunderstanding what you are doing there, but it looks to me like you are copying customer A's name to customer B, etc, but leaving the rest of the struct unchanged, so A's name ends up with B's money, and vice versa. If I'm right about that, you'd better rethink it.

    As far as the char/int question is concerned, here's a dumb little program you can play around with that illustrates my point:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
                                                                                    
    int main(void)
    {
            char str1[] = "BERRY";
            char str2[] = "Banana";
                                                                                    
            if(str1[1]<str2[1])
                    printf("%s is first.\n",str1);
            else printf("%s is first.\n",str2);
                                                                                    
    /* Note that in the next two lines, the only difference is %c vs %d */
            printf("The first character of \"BERRY\" (str1[1]) is %c\n",str1[1]);
            printf("The first character of \"BERRY\" (str1[1]) is %d\n",str2[1]);
                                                                           
            return EXIT_SUCCESS;
    }
    A character is stored in memory as an integer. If you haven't learned about that yet, look at a table of ASCII codes. There you will see a list of the integers that are used to represent each character (as well as codes with various other meanings). Then look up ASCII codes in your textbook, or google. I'm sure you'll find a better explanation than I can write in a few minutes.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    oh hell! your write about me changing their money and everything! i must be going about this all wrong!

    ok heres what i've got, a struct of up to 100 customers (which are added manually, that all works fine) then i have to sort the struct by alphabetical order (by customers name) but your right, i can't just sort the name, i need to sort everything by the name!
    i've got no idea how to do this! i've been wasting all my time! (although i suppose it wouldn't be all that different... i hope!)
    this has to be done by bubble sort.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like you need to just index it. Make an array that will hold the same number of elements as you have structures.

    "Sort" the list of structures by using a simple array of integers to reference the individual structures. Example:
    Code:
    foo[0].name is "Joe"
    foo[1].name is "Bob"
    foo[2].name is "Sue"
    Thus, your sorted index would be:
    Code:
    int index[3];
    
    index[0] = 1;
    index[1] = 0;
    index[2] = 2;
    Then you do something nifty like:
    Code:
    void displaysortedlist( struct foo list[], int index[] )
    {
        int x = 0;
    
        for( x = 0; x < TOTAL; x++ )
            printf("Name %d is %s\n", x, list[ index[x] ].name );
    
    }
    Nice and easy. Or you could just actually sort your array of stuctures. Or you could use an array of pointers to structures, and sort that.

    Probably best to pick whatever it is your assignment requires hoever.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I don't know exactly what you have there since I haven't seen your assignment or the rest of your code. But I don't think you are describing it exactly right, either.

    I think that each of your customers is a struct, and you have an array of up to 100 structs. Cust[] is the array. Cust[00] is the first struct. Cust[01] is the second struct. Right? (And if not, well, that is what you SHOULD have. )

    Well, the good news is that, while you can't assign one array to another using =, you CAN assign one struct to another using =. So if you think about that for a while, you should be able to adapt your bubble sort to sort the array of structs just the same as you can use it to sort an array of integers. Just remember that although you are comparing just the names to determine who comes first, when you swap, you have to swap the structs, not the names.

    Back to work, dude.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 03:29 PM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  3. Sorting Linked List Problem
    By chriscolden in forum C Programming
    Replies: 8
    Last Post: 01-17-2006, 09:46 AM
  4. Sorting strings like humans do?
    By electrolove in forum C Programming
    Replies: 4
    Last Post: 04-24-2003, 09:18 PM
  5. help with my bubble sorting of arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21