Segrating postive and negative numbers using bubble sort concept | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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

27th Jan 2022, 4:34 AM
Brat__
Brat__ - avatar
13 Answers
+ 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; }
27th Jan 2022, 8:46 AM
Ipang
+ 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
27th Jan 2022, 8:43 AM
A͢J
A͢J - avatar
+ 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 ...
27th Jan 2022, 8:45 AM
Ipang
+ 1
A͢J hey listen listen!! we don't have to change order yr😩
27th Jan 2022, 8:50 AM
Brat__
Brat__ - avatar
+ 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
27th Jan 2022, 8:59 AM
Brat__
Brat__ - avatar
+ 1
Lill__brattiee Done using while loop
27th Jan 2022, 9:47 AM
A͢J
A͢J - avatar
+ 1
A͢J 🙏
27th Jan 2022, 10:30 AM
Brat__
Brat__ - avatar
0
Ipang 😶we didn't have to use extra array sorry forgot to mention Anyway tysm 😁
27th Jan 2022, 8:50 AM
Brat__
Brat__ - avatar
0
Lill__brattiee Try to write a clean code as I did so it would be easy to understand.
27th Jan 2022, 8:55 AM
A͢J
A͢J - avatar
0
A͢J can u luk into once again plzzz we have to take order in preference plz plz
27th Jan 2022, 8:57 AM
Brat__
Brat__ - avatar
0
Lill__brattiee Which order?
27th Jan 2022, 8:57 AM
A͢J
A͢J - avatar
0
Lill__brattiee Ok got it we should not change order. We just have to change position of elements?
27th Jan 2022, 8:58 AM
A͢J
A͢J - avatar
0
A͢J yeaaa we just have to place all the negative numbrs first and then postive .. sorry for ur inconvenience
27th Jan 2022, 9:00 AM
Brat__
Brat__ - avatar