Thread: bubble sort with an array

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    Thumbs up bubble sort with an array

    i need to sort the array of int id[10]. this is how is see it in the book but it does do anything. it compiles with no problem but doesnt change any order. any help i would greatly apreciate it. thank you.



    Code:
    void CD::bubblesort(int id[10],int last)
    {
    for(int current=0;current<last;current++)
    bubbleup(id,current,last);
    return;
    }
    
    void CD::bubbleup(int id[10],int current, int last)
    {
    	for(int walker=last;walker>current;walker--)
    		if(id[walker]<id[walker-1])
    		{
    			int temp=id[walker];
    			id[walker]=id[walker-1];
    			id[walker-1]=temp;
    		}
    		return;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Are you sure it doesn't sort? How are you calling these methods?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    2
    actually now i tried with a breakpoint and ran it without debugging and it seems like it doesn't even run through the sort because it doesnt stop there.heres how i call it in a switch. thanks for looking at it.

    Code:
    case 2:  
    	{	ifstream infile;
    	infile.open("file2.txt");
    	getCD(infile);
    	infile.close();
    	bubblesort(id,last);
    	
    	}
    break;

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You can probably get the answer with just a little bit of debugging. I think your sort method should work. Just set a breakpoint early on and then step over each statement (or step into if necessary) and see where it is failing. Just watch your variables and make sure they are what you expect them to be. Without seeing more code it's hard to say what the problem is.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble Sort Query
    By coolboarderguy in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 12:50 AM
  2. bubble sort help.
    By zeromx in forum C Programming
    Replies: 9
    Last Post: 10-30-2006, 07:37 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM
  5. the bubble sort doesn't work help
    By Matt in forum C Programming
    Replies: 2
    Last Post: 12-13-2001, 06:14 PM