Thread: Help implementing bubble sort

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    22

    Help implementing bubble sort

    This is something that I have been working on but when I compile it I get the can error that says for the line
    Could anyone tell me how to fix this?
    Last edited by c++prog; 11-07-2009 at 12:30 AM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Your prototype is wrong:
    Code:
    void bubblesort(double, int);
    Here's your implementation:
    Code:
    void bubblesort(double *arraya,int length)//Bubble sort function
    See the difference?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    Quote Originally Posted by rags_to_riches View Post
    Your prototype is wrong:
    Code:
    void bubblesort(double, int);
    Here's your implementation:
    Code:
    void bubblesort(double *arraya,int length)//Bubble sort function
    See the difference?
    No, is it because I'm using a pointer? Because even using the bubblesort function that I have commented out I still get the same error and I am not using a pointer in that one.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    I changed the function header to void bubblesort(double *arraya, int length); and now it will compile but it won't sort the array.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well for one that's not bubblesort. Bubblesort does not use both for loop variables in the inner loop. Instead it uses i-1 or i+1 to compare element i to its neighbour, which causes items to appear to bubble up through the array, and more crucially BubbleSort is stable, whereas that is not.
    What you have is also common, but is a different algorithm (apart from the fact that it's slightly broken as well).

    If you really mean to implement bubblesort then the best thing you can do is to go and look it up on wikipedia or elsewhere.
    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
    Feb 2008
    Posts
    22
    Nevermind, I've figured it out by looking up information online and editing the function. Thanks to everyone.
    Last edited by c++prog; 11-07-2009 at 12:14 AM.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Classy, removing all your code.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    The reason I removed all my code was because this was for an assignment and I didn't want anyone to cheat off of it. If you want to see what I have now I can private message it to you.
    John 3:16 For God so loved the world that He gave His only begotten Son, that whoever believes in Him should not perish but have everlasting life.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    No one cares about your particular implementation of bubble sort dude, especially since it doesn't even work. And it's not that "we want to see what you have" it's just that you've removed all of the discussion's context.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This is why I sometimes quote the entire original post. Next time I'm doing that.
    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"

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. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  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, 04:54 PM