# Thread: sorting alphabets...

1. ## 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. 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. Why not try it out and see?

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

4. because I haven't finished the code entirely, I just wanted to see what everyone thought before I went on...

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

6. 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. 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. also, is

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

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

9. Originally Posted by dcwang3
also, is

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

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

--
Mats

10. 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. Originally Posted by dcwang3
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. 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. 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. 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. 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!

Popular pages Recent additions