Hello i have to write Bubble-sort with 3 functions in a class:
1. a function to implement the sort.
2. a function to swap the numbers in the array.
3. and the last one must give me in the output like that:
You need to pass the parameters by reference, not by value.
By the way, it's a good idea to limit the scope of variables as narrowly as possible. That is to say, rather than using an external variable n, instead use a local variable declared within the scope of the function. Doing so leads to more reliable code, as there are no unexpected side-effects elsewhere. It also means the reader doesn't need to look somewhere else to see how n is defined - is it a string or a double or what.
1 2 3 4 5 6
void sort::swap(int& a,int& b)
{
int n = a;
a = b;
b = n;
}
There are different possibilities for implementing the bubble sort algorithm. There should always be an inner and an outer loop. That is two loops. Your code uses a single loop, hence it is incomplete.
Try looking up "algorithm for bubble sort" using google if unsure - or follow your own textbook or course notes.