Thread: How do I bubble sort alphabetically?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    How do I bubble sort alphabetically?

    OK, I have a bubble sort function, which i've adapted to sort a list of names alphabetically. But the function won't sort past the first character in each word, so I get a list of names beginning with a, names beginning with b etc... The function won't distinguish between say 'alex' or 'adam'. Is there a simple way I can fix this? My Bubble sort code is:
    Code:
    int u,v;
    for(v=1; v<i; v++)
    for(int j=v-1; j>=1; j--)
    {	
    	if(strcmp(value[j-1].f_name , value[j].f_name)>0)
    	{	
                                    u = *value[j-1].f_name;
    		*value[j-1].f_name = *value[j].f_name;
    		*value[j].f_name = u;
    	}
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have to post a little more of the code around the area. I don't think you are doing the right swap function [I know for a fact that it's somewhat broken, because you are presuming not storing names in an int], but without seeing how the value array is defined, and the sort function called, it's not possible to explain exactly how much you are getting it wrong.

    --
    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.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    u could also separate out a function that compares two strings (for which one should be in front), and call it to do the comparisons in the loop. IMHO it makes the code more readable and easier to debug (you could easily feed the comparison function with pair test cases to see if the behaviour is right).

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    1
    Quote Originally Posted by arih56 View Post
    Code:
    int u,v;
    for(v=1; v<i; v++)
    for(int j=v-1; j>=1; j--)
    {	
    	if(strcmp(value[j-1].f_name , value[j].f_name)>0)
    	{	
                                    u = *value[j-1].f_name;
    		*value[j-1].f_name = *value[j].f_name;
    		*value[j].f_name = u;
    	}
    }
    i just want to comment on " *value[j-1].f_name* i think it should be (*value[j-1]).value ?..

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Please don't revive ancient threads without any good reason. See the timestamps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM