Bubblesort with a class

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:

1
2
3
4
5
6
7
8
9
10
9 3 5 6 1 7 2 0 8 4
the sort ...
1: 3 5 6 1 7 2 0 8 4 9
2: 3 5 1 6 2 0 7 4 8 9
3: 3 1 5 2 0 6 4 7 8 9
4: 1 3 2 0 5 4 6 7 8 9
5: 1 2 0 3 4 5 6 7 8 9
6: 1 0 2 3 4 5 6 7 8 9
7: 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

so i did 3 funktions
the sort.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<iostream>
#include"sort.h"
using namespace std;

void sort::sor(int a[])
{
	for(j=0;j<10;j++)
{
	if(a[j]>a[j+1]){
	swap (a[j],a[j+1]);
	print(a);}
}}
void sort::swap(int a,int b)
{
	n=a;
	a=b;
	b=n;
	}
void sort::print(int a[])
{
for(j=0;j<10;j++)
{
cout<<a[j]<<" ";}
cout<<endl;
	
}

the sort.h:
1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
class sort{
private:
int j,i,n;
public:
void sor(int a[]);
void swap(int a,int b);
void print(int a[]);
};

the main:
1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
#include"sort.h"
int main()
{
int a[10]={9,3,5,6,1,7,2,0,8,4},b,c;
sort bubble;
bubble.sor(a);
return 0;
}

but im getting in the output
9 3 5 6 1 7 2 0 8 4
could anyone say to me where the problem is?or what i have to correct?
Last edited on
This function doesn't change a or b:
1
2
3
4
5
6
void sort::swap(int a,int b)
{
    n=a;
    a=b;
    b=n;
}

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;
}

See Arguments passed by value and by reference
http://www.cplusplus.com/doc/tutorial/functions/
but it is giving
1
2
3 9 5 6 1 7 2 0 8 4 
3 9 5 6 1 7 2 0 8 4 

i think there is a problem with matching between the file sort.cpp and the sort.h and the main and i can't discover it
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.
i added a second loop in the sor funktion
1
2
3
4
5
6
7
8
void sort::sor(int a[])
{
	for(j=0;j<10;j++)
{	for(i=j+1;i<10;i++)
	{if(a[j]>a[i]){
	swap (a[j],a[i]);
	print(a);}}
}}

but it didn't change anything
it is giving me the same output
1
2
3 9 5 6 1 7 2 0 8 4 
3 9 5 6 1 7 2 0 8 4 
sry i have to ask. this is just a to big coincidence .Do you study EE at the FH Aachen ?
yes
i added a second loop in the sor funktion

That version looks reasonable to me - it should certainly do something more than the earlier version.

Maybe you need to recompile the entire project.
i rewrote the code:
the sort.h:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
class bubsort{
    int *arr,n,s,i,j;
    public:
    bubsort(int b[],int f);
    void print();
    void sort();
    void swap();
};

the sort.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include"sort.h"
using namespace std;
bubsort::bubsort(int b[],int f)
{arr=b;
n=f;}
void bubsort :: print(){
    cout<<"\n--Display--\n";
    for( i=0;i<n;i++)
        cout<<arr[i]<<"  ";
	cout<<endl;
}

void bubsort :: sort(){

    for(j=0;j<n;j++){
    for(i=j+1;i<n;i++){
    if(arr[j] > arr[i]){
    swap();
    cout<<arr[j]<<"  ";
    }
    }
    cout<<endl;
    }
}
void bubsort::swap()
{  
s = arr[j];
    arr[j] = arr[i];
    arr[i] = s;}

the main:
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include"sort.h"
using namespace std;
int main(){
    cout<<"\n*****Bubble Sort*****\n";
    int a[10]={9,3,5,6,1,7,2,0,8,4};
    bubsort obj(a,10);
    obj.sort();
    obj.swap();
    obj.print();
return 0;
}

but now it is giving me in the output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
*****Bubble Sort*****
3  1  0  
5  3  2  1  
6  5  3  2  
6  5  3  
7  6  5  4  
7  6  5  
7  6  
8  7  
8  


--Display--
0  1  2  3  4  5  6  7  8  9 

could you say to me where the problem is?
In "void bubsort :: sort()" why do you use cout inside the scope of the if-statement?
Possibly this becomes more clear after you fix your indenting.

Personally I would first completed the inner loop and then use the "void bubsort :: print()"-function.
Last edited on
Topic archived. No new replies allowed.