CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    75

    How to use bubble sort

    Hello
    Just want to ask how can i use my function now in the main ... after i put the numbers for example i want them to be sorted in ascending or descending order


    PHP Code:

    # include <iostream>
    using namespace std;
    int main () {

        
    int array [10]; 
        
    cout << " enter numbers : ";
        for ( 
    int i=0i<10i++)
            
    cin >> array[i];

        
    cout << " the numbers after sort : "<<bubblesort();

    }

    void bubblesort int list[], int length ) {
        
    int step index temp;

        for ( 
    step =1step<lengthstep++) 
        {
            for ( 
    index=0index<length-stepindex++)
                if ( list [
    index] == list [ index+])
                    
                    
    temp = list [ index];
                    list [
    index] = list[index+1];
                    list[
    index+1] = temp;
        }


  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to use bubble sort

    Did you write this yourself, or copy paste it from somewhere?

    I ask, because your code is mostly correct everywhere, yet you choke on things you have already proved you know...?

    1. You will need to forward declare your "bubblesort" function, so the compiler knows what "bubblesort" when you call it.

    2. Call bubble sort before trying to output your array. Writting "cout << bubblesort" makes no sense at all.

    3. The arguments to your bubble sort are your array, and its size.

    4. To output your array, you need to loop over its elements. You did it for input, why didn't you do it for output?

    5. I recommend you always put braces {} after an if statement. This will avoid the bugs such as the ones you have right now. Your indentation does NOT represent what is really going on.

    6. if ( list [index] == list [ index+1 ]) Are you sure that's equality you want to test?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Jul 2010
    Posts
    75

    Re: How to use bubble sort

    Our doctor wrote the function ,, and i wanted to use it , i did find the mistake like > greater then and the declaration

    PHP Code:

    # include <iostream>
    using namespace std;
    void bubblesort int array[] , int length );
    int main () {

        
    int array [10]={10,20,30,40,50};
        for ( 
    int i=0i<10i++)
        
    cout <<bubblesort(array,10);

        return 
    0;

    }

    void bubblesort int array[], int length ) {
        
    int step index temp;

        for ( 
    step =1step<lengthstep++) 
        {
            for ( 
    index=0index<length-stepindex++)
                if ( array [
    index] > array [ index+])
                {
                    
    temp = array [ index];
                    array [
    index] = array [index+1];
                    array [
    index+1] = temp;
                }
        }


  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to use bubble sort

    I'd help, but at this point, I think it would be best if you took the time to stop, and review the basics.

    Acquire some solid foundations, don't just write code hoping it will work.

    Please start here: http://www.cplusplus.com/doc/tutorial/

    If after that, you still don't know how to do what you need, then please come back and tell us what it is you are having trouble with, and we'll take all the time in the world to help you.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Jul 2010
    Posts
    75

    Re: How to use bubble sort

    Okay i will go through all the steps there =)
    Thanks Alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured