### Bubble sort It come out as 100,0,0,0....

 ``1234567891011121314151617181920212223242526272829`` ``````#include using namespace std; int main() { int ten=10, holder = 0; int TenArray [] = {100,39,90,29,33,4,2,1,900,212}; for (int index = 0; index < ten ; index ++) { if (TenArray [index] < TenArray [index + 1]) { TenArray [index + 1] = holder; TenArray [index] = TenArray [index + 1]; TenArray[index] = holder; } } for (int index = 0 ; index < 10; index ++) { cout << TenArray [index] << ", " << endl; } return 0; }`````` well, sure you get this...
I didnt get what you are trying to do in lines # 15,16,17....
you are basically assigning holder =0 to every element of array except index 0.... you basically need to swap adjacent elements in 'if' body right...use this...

holder=TenArray[index];
TenArray[index]=TenArray[index+1];
TenArray[index + 1]=holder; here is what I try to do:

I am trying get the 39 to holder and 100 move to TenArray[index]

But the outcome of this code is 100,0,0.... If i dont put holder=0 the xcode wont run I am trying get the 39 to holder

However, you write: `TenArray [index + 1] = holder;`
You are trying to assign the current value of holder (0) into array position index+1, i.e. overwrite the 39 rather than store it.

Assignment operator copies value from the right to the left.

Besides, when index==0, this `if (TenArray [index] < TenArray [index + 1])`
is effectively this `if ( 100 < 39 )` and that is why the 100 is the only one that survives the carnage. how would i fix? I am new to this bubble sort thing ``1234567891011121314151617181920212223242526272829`` ``````#include using namespace std; int main() { int ten=10, holder = 1; int TenArray [] = {100,39,90,29,33,4,2,1,900,212}; for (int index = 0; index < ten ; index ++) { if (TenArray [index] > TenArray [index + 1]) { holder = TenArray [index + 1]; TenArray [index + 1] = TenArray [index] ; TenArray[index] = holder; } } for (int index = 0 ; index < 10; index ++) { cout << TenArray [index] << ", " << endl; } return 0; }``````

This time is swap but only 39 and 100. the rest is still same Topic archived. No new replies allowed.