# How do I bubble sort alphabetically?

• 12-13-2007
arih56
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;         } }```
• 12-13-2007
matsp
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
• 12-13-2007
cyberfish
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).
• 02-27-2008
jhazeph
Quote:

Originally Posted by arih56
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 ?..
• 02-27-2008
cyberfish
Please don't revive ancient threads without any good reason. See the timestamps.