Write your question here.
I have written bubble sort function for a vector passed in by reference in two different ways, but they do not work. What is wrong with them?
In the top function the vector will stop sorting prematurely if the last 2 entries of the vector are already sorted properly (lower left, higher right)
Why are you comparing <= instead of just <? Identical values really don't need to be swapped.
The first one is almost right, it will work if you change:
1 2 3
while (sortDone == 0) {
for (int i = 0; i < vector2.size() - 1; i++) {
sortDone = 1;
to
1 2 3 4
while (sortDone == 0) {
sortDone = 1;
for (int i = 0; i < vector2.size() - 1; i++) {
You only should set done once, not every time you check elements. If they swap even once then you need to iterate again. And yes don't check = too, it will cause infinite loops when elements are not unique.