Helo with bubblesort?

This is a discussion on Helo with bubblesort? within the C Programming forums, part of the General Programming Boards category; Hi, I have written a structure of 5 stocks allowing user to enter info on 5 stocks, the code below ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    40

    Helo with bubblesort?

    Hi,

    I have written a structure of 5 stocks allowing user to enter info on 5 stocks, the code below is the function I have attempted to write a function using bubblesort to sort the stock names inputed in alphabetical order from A to Z. Did I do it right? And how to get the program to print out the result off the sort (i.e., the names after it gone thru the bubblesort?)

    Code:
    void sortStock(struct Stock s[], int n)
    {
    	int i, j, k;
    	char t[9];
    	
    	for(i=0; i<n-1; i++)
    		for(j=0; j<n-1; j++)
    			if (strcmp(s[j].stock_name, s[j+1].stock_name)>0)
    			{
    				strcpy(t, s[j+1].stock_name);
    				strcpy(s[j].stock_name, s[j+1].stock_name);
    				strcpy(s[j+1].stock_name, t);
    			}
    
    	         //    for(int k = 0; k<n; k++)
    		//	printf("%s", s[SIZE]);
    		//	printf("\n\n");
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Looks ok except for the check condition of the 2nd loop
    Code:
    for(j=0; j<n-1-i; j++)
    and the arguments of the first strcpy(), as in
    Code:
    strcpy(t, s[j].stock_name);
    ...
    Last edited by itCbitC; 04-29-2010 at 06:46 PM. Reason: tagged

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    why the "-i"? I had done the bubblesort with regular integers and n-1 was all I needed to get the proper number of times the sorting will be done to get all the values properly sorted.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by x2x3i5x View Post
    why the "-i"? I had done the bubblesort with regular integers and n-1 was all I needed to get the proper number of times the sorting will be done to get all the values properly sorted.
    Once you get the largest element at the end of your array, you don't make a full pass through it again, but stop one short. Then once you get the two largest elements at the end of your array, you don't make a full pass through it again, but stop two short. Etc.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    the n-1-i bit is not as important here. "Correctness before efficiency"
    The problem, as identified by itCbitC, is that your swap is broken. Walk through the three lines that perform the swap in your head, and if that doesn't work, do it on paper, and if you still don't notice the problem, make some good use of a debugger.
    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"

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Good catch, iMalc. The swapper portion has a flaw. I wouldn't have caught that... I've never seen it wrong before. But easy enough to do given the unusual order of arguments for noobs.

    Check source / destination in each strcpy, x2x3i5x.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Ok, made a revision on the code. I realized I wanted stock name in [j] is to be saved in t before we check the next stock name, which is in [j+1] against the stock name in [j]. t is there just in case we need to swap the stock names in [j] and [j+1]. Also, I added a third for loop, did I do that right? I changed it from what I had in the original post. This one I have here should properly print out to screen the list of stock names that should now be alphabetical order from A-Z?


    Code:
    void sortStock(struct Stock s[], int n)
    {
    	int i, j, k;
    	char t[9];
    	
    	for(i=0; i<n-1; i++)
    		for(j=0; j<n-1-i; j++)
    			if (strcmp(s[j].stock_name, s[j+1].stock_name)>0)
    			{
    				strcpy(t, s[j].stock_name);
    				strcpy(s[j].stock_name, s[j+1].stock_name);
    				strcpy(s[j+1].stock_name, t);
    			}
    	for(k=0; k<n; k++)
    			printf("%s\n", s[k].stock_name);
    }
    Last edited by x2x3i5x; 04-30-2010 at 11:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modifying bubblesort function
    By tenhavenator in forum C Programming
    Replies: 3
    Last Post: 11-13-2009, 11:54 PM
  2. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 08:20 PM
  3. scanf in bubblesort
    By agentsmith in forum C Programming
    Replies: 3
    Last Post: 12-18-2007, 04:09 PM
  4. Circular bubblesort
    By PiCoMiKe in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 06:06 PM
  5. C++ bubblesort and structure help
    By ReignFire in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 02:25 AM

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