954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

simplified bubble sort c++

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

d1e9v85
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

what does this program do?

z3r0acidk
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You