hi guys ...

this is a bubble sort program that i made

#include<iostream>
using namespace std;

#include<iomanip>
using std::setw;

int main()

{
    const int arraysize = 10;
    int Bubble[ arraysize ];

    cout << "Please enter 10 integers: " << endl;

    for ( int i = 0; i < arraysize; i++ )
        cin >> Bubble[ i ];

    
    cout <<"unsorted array:\n";


    for ( int j = 0; j < arraysize; j++ )
        cout << setw( 4 ) << Bubble[ j ];

        for(int y=0; y < arraysize; y++)

        {

            for ( int k =0; k < arraysize -1-y; k++ )

            if(Bubble[k]>Bubble[k+1])

            {

                int temp = Bubble[k+1];

                Bubble[k+1] = Bubble[k];

                Bubble[k] = temp;

            }

        }


      cout << "\nSorted Bubble array:\n";

    for (int l = 0; l < arraysize; l++ )
        cout << setw( 4 ) << Bubble[ l ];

    cout << endl;
    return 0;
}

now the problem i am getting is that :
the data in the array may be already in the proper order or near proper order, this can be done in fewer passes than nine...
how can i modify this sort to check at the end of each pass if any swaps have been made. if none have been made then the data must been in proper order so the program should terminate if the swap has been made then atleast one more pass is needed


please helpp

thank you

Recommended Answers

All 8 Replies

create a bool flag before the loop starts and set it to false. when a swap is made change the flag to true. on next loop iteration if the flag is still false then no swaps were performed during the previous loop iteration.

and please next time use code tags.

You cant as such check for such conditions in case of a simple sort like bubble sort.

Take for eg. this array.
1 2 3 4 5 6 8 7

During the first seven passes no swap is performed since the next number is always greater than the current but a final swap is done in the last pass to sort the array. You cant as such terminate a bubble sort based on the flag that swap is performed or not.

You cant as such check for such conditions in case of a simple sort like bubble sort.

Take for eg. this array.
1 2 3 4 5 6 8 7

During the first seven passes no swap is performed since the next number is always greater than the current but a final swap is done in the last pass to sort the array. You cant as such terminate a bubble sort based on the flag that swap is performed or not.

Yes you can. You set the swap flag to FALSE between the two loops (inside the outer loop, before the inner loop). When the inner loop exits and a swap has been made, the swap flag should be TRUE. If no swap has been made, the flag should be FALSE and it's safe to exit.

what does this program do?

I do not understand why did you make 3 for loops?

This impelementation also works fine.

/*
* BubbleSort.cpp
*
*  Created on: May 24, 2013
*      Author: mfoua_000
*/
#include<iostream>

using namespace std;

void swap(int &i, int &j);

int main(int argc, char **argv) {
    int data[] = { 5, 4, 1, 8, 7, 2, 6, 3 };

    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8-1-i;j++)
        {
            if(data[j]>data[j+1])
                swap(data[j],data[j+1]);

        }
    }

    for(int i=0;i<8;i++)
        cout << data[i] << " ";

    cout << endl;

    return 0;
}
void swap(int &i, int &j)
{
    int temp = i;
    i = j;
    j = temp;
}

I do not understand why did you make 3 for loops?

And I do not understand why you responded to a 6-year-old thread

I do not understand why did you make 3 for loops?

The real question would be why did the OP not format his code in an understandable way. Reread the original code, it doesn't have three nested loops.

HELLLLLOOOOOOOOOOOO

Yeah seriously why did he use THREE DAMN LOOPS!
like 1
and like 2
and then like 3
like wowow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.