bubble sort

This is a discussion on bubble sort within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have a program where I have to sort array of structures by the month of the birthday. ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    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

    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);
    	
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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?
    Last edited by Adak; 04-14-2009 at 07:16 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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
    Thank you for your help!!!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Quote Originally Posted by nynicue View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you guy's, you are the best.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 01:30 AM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 03:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21