Ppl,
I am attempting to sort a list of names..how could I go about that?
A
Ppl,
I am attempting to sort a list of names..how could I go about that?
A
Use your favorite sorting method except with strcmp for the comparisions and strncpy for the swapping.
-Prelude
My best code is written with the delete key.
Umm..what would some of the code look like?
A
P.S: Thanks already!
>Umm..what would some of the code look like?
It depends on what the list of names looks like. Is it a linked list or an array of pointers? But I get the feeling that you will find plenty of code by searching these boards for sorting keywords. It saves me the trouble of writing a new sorting routine or scrounging around for one of the ones I've collected.
-Prelude
My best code is written with the delete key.
Hi,
This is what I've got so far..but for some reason before it sets the entire list it crashes..I have 16 names i.e rows = 16..someone please help debug
void sortLastNames(char** strings,
int rows)
{
/* Local Definitions */
int i;
char temp;
printf("SORTED STRINGS:\n");
for(i=0; i < rows; i++)
{
if(strcmp(strings[i], strings[i+1])>0)
{
temp = strings[i];
strings[i] = strings[i+1];
strings[i+1] = temp;
}
printf("%s\n", strings[i]);
}
//end of sort
}
Perhaps you should pay attention to your compiler's warning messages.
> temp = strings[i];
You should be getting warnings about this
Make
char *temp;
to fix this
> strcmp(strings[i], strings[i+1])>
The i+1 ensures you step off the end of the array when you do the last iteration of the loop
> void sortLastNames(char** strings
Is this really the type of the array you're passing to this function.
char *strings[10]; // good
char strings[10][10];// bad
One more thing, this only sorts the last element of the array
Search the web for more detail on how to implement bubble sorts