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