bubble sort
Jul 12, 2013 at 12:44am UTC
So I'm doing one of these bubble sort programs and it is certainly sorting for one pass. It will pass the highest integer to the end but then is not doing successive passes. I'm not really sure why. Help!
Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <iomanip>
int main()
{
const int arraySize = 10; // size of array a
int myArray[ arraySize ] = { 37, 89, 12, 8, 10, 4, 6, 68, 45, 2 };
int temp;
int j;
std::cout << "Data items in original order\n" ;
// output original array
for ( int i = 0; i < arraySize; i++ )
std::cout << std::setw ( 4 ) << myArray[ i ];
for (j=0; j < 10; j++)
{
for (j=0; j < 9; j++)
{
if (myArray[j] > myArray[j+1])
{
temp = myArray[j+1];
myArray[j+1] = myArray[j];
myArray[j] = temp;
}
}
}
Jul 12, 2013 at 1:05am UTC
I think you need to use a different loop variable for your inner loop.
Jul 12, 2013 at 1:18am UTC
Good eye and thanks! It's working now. I had something similar going on earlier but some goofy notes had led me astray. Thanks.
Topic archived. No new replies allowed.