- 1
Segrating postive and negative numbers using bubble sort concept
I have used the method of bubbling but it keep showing me error...if any of u have a little idea plzz amend it plzz🥺 https://code.sololearn.com/chrRfev4AwH8/?ref=app
13 Respuestas
+ 4
void bubble( int* p, size_t items )
{
int* temp = new int[ items ];
for( size_t i { 0 }, R { 0 }, C; R < items; R++ )
{
for( C = 0; C < items; C++ )
{
if( R )
{
if( p[ C ] >= 0 ) // copy positive values
{
temp[ i++ ] = p[ C ];
}
}
else
{
if( p[ C ] < 0 ) // copy negative values
{
temp[ i++ ] = p[ C ];
}
}
}
}
// copy values from <temp> back into <p>
for( size_t i { 0 }; i < items; i++ )
{
p[ i ] = temp[ i ];
}
delete [] temp;
}
+ 1
Lill__brattiee
The problem was with inner for loop
You did j = i + 1 but j <= 0 which will never execute.
some little changes done
https://code.sololearn.com/cFhadC7PZ8XE/?ref=app
+ 1
I managed to do this with the help of a temporary array in the bubble() function.
Basically, using a nested loop, first we copy negative values, followed by positive values into the temporary array. When we're done, copy all items in temporary array back into original array.
There's a little bit of change in the bubble() forward declaration
void bubble( int*, size_t );
Implementation below ...
+ 1
A͢J hey listen listen!! we don't have to change order yr😩
+ 1
A͢J the order in which we enter numbers...that shouldn't be change ...we have to just consider postive numbers and negative aside
+ 1
Lill__brattiee
Done using while loop
+ 1
A͢J 🙏
0
Ipang 😶we didn't have to use extra array sorry forgot to mention Anyway tysm 😁
0
Lill__brattiee
Try to write a clean code as I did so it would be easy to understand.
0
A͢J can u luk into once again plzzz we have to take order in preference plz plz
0
Lill__brattiee
Which order?
0
Lill__brattiee
Ok got it we should not change order. We just have to change position of elements?
0
A͢J yeaaa we just have to place all the negative numbrs first and then postive .. sorry for ur inconvenience