Bubblesort

Pages: 12
Hallo i have to wrie Bubblesort with 3 funktions in a class
a funktion to impelement the sort ,a funktion to swap the numbers in the array
and the last one must give me in the output like that:
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 files
for funktions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include"sort.h"
using namespace std;
void sort::sort(int f[i])
{a[i]=f[i]}
void sort::sor()
{
	if[code](a[j]>a[i]){
	swap (a[i]);
}}
void sort::swap()
	{
	for(j=0;j<10;j++)	
	for(i=j+1;i<10;i++)
{
	n=a[j];
	a[j]=a[i];
	a[i]=n;
	}	}
void sort::print()
{
	
}

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

return 0;
}

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

can anyone help me please?
Last edited on
You aren't trying to do a normal "class", since you are using it as a sort, you are doing a functor (a function object). Also don't use bubble sort, it's a bad type of sort that has almost no realistic application, as its average time complexity is O(n^2).

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
template<typename data, size_t size>
class functor
{
 public:
   functor()
   {};
   
   void operator()(data (&arr)[size])
   {
       bool is_sorted=false;
       
       while(!is_sorted)
       { 
        is_sorted=true;
        for(int unsigned i=0; i<size-1;++i)
        {
        if(arr[i]>arr[i+1]) 
          {
            swap(arr[i],arr[i+1]);
            is_sorted=false;
          }
        }
      }
   };
   
   void swap(data & a, data & b)
   {
       a^=b;
       b^=a;
       a^=b;
   };
   
  void print(data (&arr)[size])
   {
        for(int i=0; i<size;++i)
        std::cout<<arr[i]<<' ';
   }
};

int main()
{
    int x[]={1,5,9,2,8,0};
    functor<int,sizeof(x)/sizeof(x[0])> bubble;
    bubble(x);
    bubble.print(x);
  
    return 0;
}
Last edited on
i have to write the program bubblesort with 3 funktions in a class without template, just funktions and class then i'll call it in the main
Then, use an int array as a field for the class and initialize it inside the constructor. Then sort that array, the functions are pretty much the same.
Last edited on
@Golden Lizard
I've seen this style a few times recently. It's impressively geeky, but doesn't make much sense in the context of a templated function. For example if main looked like this, then it would fail to compile
1
2
3
4
    std::string y[]={ "1", "5", "9", "2", "8" ,"0" };
    functor<std::string,sizeof(y)/sizeof(y[0])> bubble;
    bubble(y);
    bubble.print(y);

Though there is of course std::swap, when writing code as an example for a beginner, it's better to stick with something more simple:
1
2
3
4
5
6
   void swap(data & a, data & b)
   {
       data temp = a;
       a         = b;
       b         = temp;
   };


@kais2 Apologies for the distraction.

I took a look at your code, and it seems that there are a number of errors.

I would start with something simple, then build up gradually, one step at a time. Here's a start, with a constructor which takes an array of integers and its size.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class sort {
private:
    int size;    // number of items in the array 
    int *arr;    // pointer to the array of integers
    
public:
    sort(int array[], int length);
    
};

sort::sort(int array[], int length)
{
    arr  = array;
    size = length;
}

int main()
{
    int a[10] = {9,3,5,6,1,7,2,0,8,4};

    sort bubble(a, 10);
    
}


Next, I suggest adding the print function - that will be useful throughout all the remaining steps.

The swap function will be pretty simple, just three lines.

The sort function will involve the loops somewhat similar to those you already have.
Last edited on
like that?
the funktions:
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
#include<iostream>
#include"sort.h"
#include<iostream>
#include"sort.h"
using namespace std;
sort::sort(int array[], int length)
{
    a= array;
    i= length;
}
void sort::sor()
{	for(j=0;j<10;j++)	
	for(i=j+1;i<10;i++){
	if(a[j]>a[i]){
	swap (a[j],a[i]);
	print()
}}
void sort::swap(int b,int r)
	{
{
	n=b;
	b=r;
	r=n;
	}	}
void sort::print()
{
for(i=0;i<i;i++)
cout<<i<<":"<<a[i]<<endl;}

the main:
1
2
3
4
5
6
7
8
9
10
11
12
#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(a,10);
bubble.sor();
bubble.swap(b,c);
bubble.print();
return 0;
}

the class:
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;
class sort{
private:
int *i,*j,n,a[];
public:
sort(int array[], int length);
void sor(int);
void swap(int ,int );
void print(int);
};
Last edited on
like that?

Well, did you test it, and does it work?
What output do you get?
I've seen this style a few times recently. It's impressively geeky, but doesn't make much sense in the context of a templated function. For example if main looked like this, then it would fail to compile


True, but you can make specialized templates for those things.
it is giving me alot of problems
like the class has a previous definition
Do you use header guards?
i had rewrote the program:
class:
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;
class sort{
private:
int j,n,*a;
public:
sort(int array[], int length);
void sor();
void swap();
void print();
};

the funktions:
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"sort.h"
#include<iostream>
#include"sort.h"
using namespace std;
sort::sort(int array[], int length)
{
    a=array;
   j=length;
}
void sort::sor()
{
	for(j=0;j<10;j++){
	if(a[j]>a[j+1]){
	swap ();
	print();}
}}
void sort::swap()
{
	n=a[j+1];
	a[j]=a[j+1];
	a[j+1]=n;}
void sort::print()
{
for(j=0;j<10;j++)
cout<<j<<":"<<a[j]<<endl;	
}

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

return 0;
}

but it is giving me that the class has a previous definition
@Golden Lizard yes im using one header guard and two cpp
header guards, not headers, like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef _SORT_H
#define _SORT_H

#include<iostream>
using namespace std;
class sort{
private:
int j,n,*a;
public:
sort(int array[], int length);
void sor();
void swap();
void print();
}
#endif 


I also see that you include "sort.h" twice inside the .cpp file, that will define the class 2 times, so it's gonna give you an error at compiling. #include is nothing magic, it just copies the contents of the header, headers are used to save compile time, so it doesn't recompile every time you change something in your code.
1
2
3
4
#include<iostream>
#include"sort.h"
#include<iostream>
#include"sort.h" 
now it is giving me this
0:3
1:3
2:5
3:6
4:1
5:7
6:2
7:0
8:8
9:4
0:3
1:3
2:5
3:6
4:1
5:7
6:2
7:0
8:8
9:4
Sor function already calls print once, you call print again in your main. So it works, only that you print it twice
Last edited on
but i want to print in the output like that:
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
Don't use endl after every element.

1
2
3
4
5
6
7
void sort::print()
{
for(j=0;j<10;j++)
std::cout<<j<<":"<<a[j]<<' ';

std::cout<<"\n";
}
Last edited on
now,it is giving me
0 1 2 3 4 5 6 7 8 9
but i want in the output
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
here is my code for the second time
the main:
1
2
3
4
5
6
7
8
9
10
11
12
13
#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(a,10);
bubble.sor();
bubble.swap();
bubble.print();

return 0;
}

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

the functions:
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<iostream>
#include"sort.h"
using namespace std;
sort::sort(int array[], int length)
{
   a=array;
   j=length;
}
void sort::sor()
{
	for(j=0;j<10;j++)
	for(i=j+1;i<10;i++)
	{if(a[j]>a[i]){
	swap ();
}
}}
void sort::swap()
{
	n=a[j];
	a[j]=a[i];
	a[i]=n;}
void sort::print()
{
for(j=0;j<10;j++)
{
cout<<a[j]<<" ";}
cout<<endl;
	
}

the output must be
1
2
3
4
5
6
7
8
9
10
9 3 5 6 1 7 2 0 8 4
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

but i'm getting just one line
0 1 2 3 4 5 6 7 8 9
can any one help me?
Last edited on
Pages: 12