Code: (java)
public static void bubble( int a[] )
{
boolean done = false;
int temp;
for( int forwardPass = 1; forwardPass < N && !done; forwardPass++ )
{
done = true;
for( int i = 0; i < N - forwardPass; i++ )
{
if( a[ i ] > a[ i + 1 ] )
{
temp = a[ i ];
a[ i ] = a[ i + 1 ];
a[ i + 1 ] = temp;
if( a[ i ] < a[ i - 1 ] )
{
temp = a[ i ];
a[ i ] = a[ i - 1 ];
a[ i - 1 ] = temp;
done = false;
}
done = false;
}
}
}
}I'm trying to write a method that will sort some values that I've put into an array.
The first "if" statement does a bubble sort forwards and I know that works. I'm trying to get the bubble sort to sort backwards after the forward pass and the 2nd "if" statement is my attempt at doing so.
It seems to me that the code provided would work accordingly but it doesn't. I tried commenting out the first "if" statement and the program stops working at the 2nd "if" statement. However, if nothing is commented out then the program sorts like it should so maybe I'm testing it wrong.


