Bubble sort not working?

Hey! I'm having a problem with my bubble sort loop which is supposed to take an array with 8 members and sort it using bubble sort. It has to output the current iteration of sorting that it's completed each round. I've gotten it to output 8 times, but the output is just 0;00 each time for me. Not sure where I've messed up.. Also ignore the selArray as it is an array I have to make be sorted by selection sort next. I'm just doing the bubble sort first.

 ``12345678910111213141516171819202122232425262728293031323334`` `````` #include using namespace std; int main() { int bubArray[] = {5,1,4,8,2,9,3,7}; int selArray[] = {5,1,4,8,2,9,3,7}; int chkA, chkB, chng, out; for(chkA = 0; chkA < 7; chkA++) { for(chkB = 0; chkB < 7 - chkA; chkB++) { if(bubArray[chkB] > bubArray[chkB+1]) { out = 0; cout << "The current array order is: "; do { cout << bubArray[out] + ","; ++out; } while (out < 8); chng = bubArray[chkB]; bubArray[chkB] = bubArray[chkB+1]; bubArray[chkB+1] = chng; cout << "\n"; } } } return 0; }``````
Last edited on
 but the output is just 0;00 each time for me

bubArray[out] is an integer, so the instruction
`bubArray[out] + ","`
doesn’t concatenate two strings, but tries to perform an arithmetic sum.

I’d personally prefer a third for-loop instead of a while-loop, because I think it would make the code more readable, but that’s just a matter of preferences.
Example:
 ``123456789101112131415161718192021222324252627`` ``````#include int main() { int bubArray[] = { 5, 1, 4, 8, 2, 9, 3, 7 }; // int selArray[] = { 5, 1, 4, 8, 2, 9, 3, 7 }; for(int chkA = 0; chkA < 7; ++chkA) { for(int chkB = 0; chkB < 7 - chkA; ++chkB) { if(bubArray[chkB] > bubArray[chkB+1]) { std::cout << "The current array order is: "; for(int out {}; out < 8; ++out) { std::cout << bubArray[out] << ", "; } std::cout << '\n'; int chng = bubArray[chkB]; bubArray[chkB] = bubArray[chkB+1]; bubArray[chkB+1] = chng; } } } return 0; }``````

Output:
 ```The current array order is: 5, 1, 4, 8, 2, 9, 3, 7, The current array order is: 1, 5, 4, 8, 2, 9, 3, 7, The current array order is: 1, 4, 5, 8, 2, 9, 3, 7, The current array order is: 1, 4, 5, 2, 8, 9, 3, 7, The current array order is: 1, 4, 5, 2, 8, 3, 9, 7, The current array order is: 1, 4, 5, 2, 8, 3, 7, 9, The current array order is: 1, 4, 2, 5, 8, 3, 7, 9, The current array order is: 1, 4, 2, 5, 3, 8, 7, 9, The current array order is: 1, 4, 2, 5, 3, 7, 8, 9, The current array order is: 1, 2, 4, 5, 3, 7, 8, 9, The current array order is: 1, 2, 4, 3, 5, 7, 8, 9,```

Sorry, I focused only on the output and missed the main point.
Yes, there’s an error in your Bubble-sort loop. The Bubblesort algorithm assumes the loop will go on until all the elements are in the right order.
At every iteration, you need to check if some element is still in the wrong place or not.
- If no element is in the wrong place, than that must be the last iteration.
- If there’s at least one element in the wrong place, than the loop must iterate another time.
What you did worked great! There is a problem with the output in the final line as you can see, where 3 is after 4 and before 5. Other than that it works. I went ahead and inserted my own version of the selection sort afterwards and attempted to get it to work, but it only works halfway through the sort? I'm assuming it's a problem with how I count the chk's.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include using namespace std; int main() { int bubArray[] = {5,1,4,8,2,3,9,7}; int selArray[] = {5,1,4,8,2,3,9,7}; int chkA, chkB, chng, out;//variables for the for loops for(chkA = 0; chkA < 7; ++chkA) { for(chkB = 0; chkB < 7 - chkA; ++chkB) { if(bubArray[chkB] > bubArray[chkB+1]) { std::cout << "The current array order is: "; for(out = 0; out < 8; ++out) { std::cout << bubArray[out] << ", "; } std::cout << "\n"; chng = bubArray[chkB]; bubArray[chkB] = bubArray[chkB+1]; bubArray[chkB+1] = chng; } } } int chkC, chkD, oot, ploop, chung;//Variables for the for loops cout << "\n" << "The bubble sort is finished, the selection sort will now occur" << "\n"; for(chkC = 0; chkC < 7; ++chkC) { ploop = chkC; //Sets ploop equal to the current array for(chkD = chkC + 1; chkD < 7; ++chkD) { if(selArray[chkD] < selArray[ploop]) { ploop = chkD; } } if(ploop != chkC) { std::cout << "The current array order is: "; for(chung = 0; chung < 8; ++chung) { std::cout << selArray[chung] << ", "; } std:cout << "\n"; oot = selArray[chkC]; selArray[chkC] = selArray[ploop]; selArray[ploop] = oot; } } return 0; }``````
Topic archived. No new replies allowed.