# bubble sort

• 04-14-2009
nynicue
bubble sort
Hello everyone, I have a program where I have to sort array of structures by the month of the birthday. I have a function to do that and it looks that it must work but the program stops when it comes to that function. I will be very appriciated if someone can tell me where i am wrong.
Thank you:confused:

Here is the function, I am using bubble sort.

Code:

```void sort_array_by_month(void) {         struct sStudent Temp;         int i, n, swap;         do         {                 swap=0;                 for(i=0; i<3; ++i)//bubble sort                 {                         if(Algebra[i].birthday.month>Algebra[i+1].birthday.month)                                 Temp.birthday.month=Algebra[i].birthday.month;  // temp[Count]=Array[count+1]                                 Temp.birthday.day=Algebra[i].birthday.day;                                 Temp.birthday.year=Algebra[i].birthday.year;                                 for(n=0; Algebra[i].first_name[n]!=0; ++n)                                 {                                         Temp.first_name[n]=Algebra[i].first_name[n];                                         Temp.first_name[n]='\0';                                 }                                                                 for(n=0; Algebra[i].last_name[n]!=0; ++n)                                 {                                         Temp.last_name[n]=Algebra[i].last_name[n];                                         Temp.last_name[n]='\0';                                 }                                 Algebra[i].birthday.day=Algebra[i+1].birthday.day; //Array[Count]=Array[Count+1]                                 Algebra[i].birthday.month=Algebra[i+1].birthday.month;                                 Algebra[i].birthday.year=Algebra[i+1].birthday.year;                                 for(n=0; Algebra[i].first_name[n]!=0; ++n)                                 {                                         Algebra[i].first_name[n]=Algebra[i+1].first_name[n];                                         Algebra[i].first_name[n]='\0';                                 }                                                                 for(n=0; Algebra[i].last_name[n]!=0; ++n)                                 {                                         Algebra[i].last_name[n]=Algebra[i+1].last_name[n];                                         Algebra[i].last_name[n]='\0';                                 }                                                                 Algebra[i+1].birthday.month=Temp.birthday.month;  //Array[Count+1]=Temp                                 Algebra[i+1].birthday.day=Temp.birthday.day;                                 Algebra[i+1].birthday.year=Temp.birthday.year;                                 for(n=0; Algebra[i].first_name[n]!=0; ++n)                                 {                                         Algebra[i+1].first_name[n]=Temp.first_name[n];                                         Algebra[i+1].first_name[n]='\0';                                 }                                                                 for(n=0; Algebra[i].last_name[n]!=0; ++n)                                 {                                         Algebra[i+1].last_name[n]=Temp.last_name[n];                                         Algebra[i+1].last_name[n]='\0';                                 }                                 swap=1;                        }         }while(swap!=0);         }```
• 04-14-2009
You're working very hard at this, clearly.

You don't need to work anywhere near this hard.

Structures have the "magical" property in C, of being able to be swapped, as a whole unit. That works using either a pointer to the struct being swapped, OR it works by swapping it out *directly*.

No further struct members need to even be mentioned! :) Your bubblesort shrinks by a factor of 10X.

If temp is a struct of the same type, (or a pointer to that kind of struct), then
Code:

```temp = struct1; struct1 = struct2; struct2 = temp;```
works.

And you can thank iMalc for that tip.
• 04-14-2009
nynicue
thank you for the tip I'll try to get it work, I kind of thought that it must be something more simple to sort it but our teacher told that when you sort array of structures you have to do it in the way how I did it. And thanks again
• 04-14-2009
I can sort out the problem with it, if you like. I mentioned this only because I thought it was a great tip from iMalc, and would save you a lot of work.

If you want to use this type of sort however, it can be sorted out.

What is the first key you are sorting by? Maybe name?

Do you need to sort by a second key, as well? Maybe birthdays?

Any members that we are using for a third sort key?
• 04-14-2009
nynicue
Adak I need to sort it only by the month of the birthday, I do not know if i've done this right but here is the updated version.
Code:

```#include<stdio.h> struct sdate {         int month;         int day;         int year; }; struct sStudent {         char last_name[30];         char first_name[30];         struct sdate birthday; }Algebra[10]; void get_first_name(int j) {         printf("First name    ");         scanf("%s", Algebra[j].first_name); } void get_last_name(int j) {         printf("Last name    ");         scanf("%s", Algebra[j].last_name); } void get_date_of_birth(int j) {         printf("Birthday      ");         scanf("%i%i%i", &Algebra[j].birthday.day, &Algebra[j].birthday.month, &Algebra[j].birthday.year);         printf("\n"); } int get_info(void) {         int i;                 printf("!!! Date of Birth must be entered as dd mm yy separated by space\n\n");         for(i=0; i<3; ++i)         {                 printf("Student# %i\n", i+1);                 printf("========================\n");                                 get_first_name(i);                 get_last_name(i);                 get_date_of_birth(i);         }         return 0; } void sort_array_by_month(void) {         struct sStudent Temp;         int i, n, swap;         do         {                 swap=0;                 for(i=0; i<3; ++i)//bubble sort                 {                         if(Algebra[i].birthday.month>Algebra[i+1].birthday.month)                                                                 Temp=Algebra[i];                                 Algebra[i]=Algebra[i+1];                                 Algebra[i+1]=Temp;                                 swap=1;                        }         }while(swap!=0);         }  main() {         int i;         get_info();         sort_array_by_month();         for(i=0; i<3; ++i)         {                 printf("%s  %s", Algebra[i].first_name, Algebra[i].last_name);                 printf("%15i/%i/%i\n", Algebra[i].birthday.day, Algebra[i].birthday.month, Algebra[i].birthday.year);         } }```
Also not sure but do I need to end string with a null character maybe like
Code:

``` Temp.first_name[n]='\0'; Temp.last_name[n]='\0';    and so on```
• 04-14-2009
If you are handling strings in a very rigid fixed field length, then you don't need an end of string char - '\0' (note the single quotation marks.

If you are using any string functions in your code, you'll need end of string char's in place.

strcmp(), strlen(), strcpy(), and printf("%s"), etc., all rely on strings being present. Only char's with an end of string marker in place, are seen as strings, by C.

You're welcome.
• 04-15-2009
iMalc
Quote:

Originally Posted by nynicue
Code:

```void sort_array_by_month(void) {         struct sStudent Temp;         int i, n, swap;         do         {                 swap=0;                 for(i=0; i<3; ++i)//bubble sort                 {                         if(Algebra[i].birthday.month>Algebra[i+1].birthday.month)                                                                 Temp=Algebra[i];                         Algebra[i]=Algebra[i+1];                         Algebra[i+1]=Temp;                         swap=1;                        }         }while(swap!=0);         }```

That looks much shorter! Now to make it correct as well... :)
One thing missing though is that of those four lines that are supposed to be inside the if statement - only the first one actually is, because there are no curly-braces surrounding those statements. I've altered the indentation above to show what it is really doing.

Second thing is that the variable n is unused. You could either delete it, or make it a parameter that is passed in. Of course in that case it would be nice to pass the array as a parameter as well, so you may choose to simply delete n. Either way it would be good to remove the magic number 3 from the function and in main.
• 04-15-2009
nynicue
Thank you guy's, you are the best.