Bubblesort Template Problems

Hey guys, I'm finishing up my final project and I'm running into some problems. The project is: Create 5 myFloat objects and 5 myName objects, print them to the output screen, then pass them through a bubble sort template to arrange them into numerical order based on x, and alphabetical order based on last name.

I have a template that works if I assign a random array of numbers and chars into it, but I'm stuck on how to pass the myFloat and myName objects into it to be sorted. Any guidance here to point me at what I need to correct would be awesome. Here's my current code:

HEADER
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
#ifndef MYFLOAT_H
#define MYFLOAT_H
#include <iostream>
#include <string>
using namespace std;

class myFloat
{
	float x;
	float y;
public:
	myFloat();
	myFloat(float, float);
	void setX(float);
	void setY(float);
	float getX();
	float getY();
	ostream friend & operator<< (ostream &os, myFloat m);
	bool operator> (myFloat);
};

class myName
{
	string fn;
	string ln;
public:
	myName();
	myName(string, string);
	void setF(string);
	void setL(string);
	string getF();
	string getL();
	ostream friend & operator<< (ostream &, myName);
	bool operator> (myName);
};
#endif 


MYFLOAT DEFINITIONS
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
49
50
51
52
53
54
55
56
#include <iomanip>
#include "myFloat.h"

myFloat::myFloat()
{
	x = 0.0;
	y = 0.0;
}

myFloat::myFloat(float a, float b)
{
	x = a;
	y = b;
}

void myFloat::setX(float a)
{
	x = a;
}

void myFloat::setY(float b)
{
	y = b;
}

float myFloat::getX()
{
	return x;
}

float myFloat::getY()
{
	return y;
}

ostream &operator << (ostream &os, myFloat m)
{
	os << " x = " << m.getX() << " y = " << m.getY() << endl;
	return os;
}

bool myFloat::operator>(myFloat m)
{
	if ( x > m.x )
	{
		return true;
	}
	if ( x == m.x || y <= m.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}


MYNAME DEFINITIONS
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
49
50
51
52
53
54
55
#include <iomanip>
#include "myFloat.h"

myName::myName()
{

}

myName::myName(string f , string l)
{
	fn = f;
	ln = l;
}

void myName::setF(string f)
{
	fn = f;
}

void myName::setL(string l)
{	
	ln = l;
}

string myName::getF()
{
	return (fn);
}

string myName::getL()
{
	return (ln);
}

ostream & operator<< (ostream & create, myName obj)
{
	create << "Name is: " << obj.getF() << " " << obj.getL() << endl;
	return create;
}

bool myName::operator>(myName m)
{
	if ( fn > m.ln )
	{
		return true;
	}
	if ( fn == m.fn || ln <= m.ln)
	{
		return true;
	}
	else
	{
		return false;
	}
}


MAIN
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
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <iomanip>
#include "myFloat.h"
using namespace std;

template<class bubble>
void bubblesort(bubble a[], int n)
{
    int i, j;
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(a[i] > a[j])
            {
                bubble element;
                element = a[i];
                a[i] = a[j];
                a[j] = element;
            }
        }
    }
}


int main()
{
	myFloat f1;
	myFloat f2(3.2, 1.6);
	myFloat f3(7.3, 8.3);
	myFloat f4(4.1, 2.7);
	myFloat f5(6.8, 9.2);

	f1.setX(1.4);
	f1.setY(5.8);

	cout << "Before Sort\n";
	cout << f1 << f2 << f3 << f4 << f5 << "\n";

	myName n1;
	myName n2("Kevin", "Davis");
	myName n3("John", "Smith");
	myName n4("Abraham", "Lincoln");
	myName n5("Derek", "Jeter");

	n1.setF("Nick");
	n1.setL("Jones");

	cout << "\n";
	cout << "Before Sort\n";
	cout << n1 << n2 << n3 << n4 << n5 << "\n";

	cout << "After Sort\n";

	return 0;
}


Again, any help is much appreciated. I'm so close to finishing this up, I just need to know how to tweak this bubble sort template. Thanks guys!
Last edited on
Quick bump before I head to bed for the night. Not even sure if I'm allowed to do this...
I am having the same problem in my project as well. I think a bubble sort rearranges the array. Im not sure this will effect the objects.
Ya, I can create an array and pass it through the bubble sort but that doesn't do me any good. I need to sort them based on what x is and based on what the last name is. I hate being so close, and yet so far.

operator > got overloaded in both classes to compare objects in the order of x for myFloat and in the order of last name, then first name for myName. I'm just lost on how to incorporate them into the bubble sort
Last edited on
You have 2 options. You either overwrite the < and > operators or you create a function that compares exactly that something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
compare(myFloat a, myFloat b)
{
  float x = a.getX();
  float y = b.getX();
  if(x == y)
  {
     return 0; //  they are equal
   }
  else if(x > y)
  {
    return 1;
   }
   else(x < y)
   {
     return -1;
   }
}

And you use this function inside your bubble sort algorithm.
You do the same thing for myName class;
Last edited on
Please let me know if this is what you're referring to. I changed my header and .cpp files to this:

Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class myFloat
{
	float x;
	float y;
public:
	// ...
	bool compare(myFloat, myFloat);
};

class myName
{
	string fn;
	string ln;
public:
	// ...
	bool compare(myName, myName);
};


myFloat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...
bool myFloat::compare(myFloat a, myFloat b)
{
	float x = a.getX();
	float y = b.getX();
	if(x == y)
	{
		return 0;
	}
	else if(x > y)
	{
		return 1;
	}
	else if(x < y)
	{
		return -1;
	}
}


myName
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...
bool myName::compare(myName a, myName b)
{
	string x = a.getL();
	string y = b.getL();
	if(x == y)
	{
		return 0;
	}
	else if(x > y)
	{
		return 1;
	}
	else if(x < y)
	{
		return -1;
	}
}


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
template<class bubble>
void bubblesort(bubble a[], int n)
{
    int i, j;
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
          

        }
    }
}


Not sure if that is what you're talking about, but if it is...how would I incorporate it into the sort exactly?
I've gone back to what my original code was in the first post. I've been reading up on bubble sort templates and I'm still as lost as I was hours ago. I know the template means you can re-use the code with different types, I'm just not sure how to implement it so I can use it to sort the x's in numerical order, and last names in alphabetical order.

Could I just put f1, f2, f3, f4, f5 into an array after creating them and then sort that? Or am I just talking out of my butt now? If anybody could explain to me a little more how I'd go about making this work that would be phenomenal!
well, in the case you wish to use it in a template, you need to overload the > and < and == operators for your class.
In this case i think someone already helped you do the overloading in a diferent thread.
As for the bubble sort for you just compare them with ">" and "<" and swap them if necessary.
As for the overloading you overloaded them wrong.
You only need to return true if the sign is corect(">" or "<").
For exemple this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool myName::operator>(myName m)
{
	if ( fn > m.ln )
	{
		return true;
	}
	if ( fn == m.fn || ln <= m.ln)
	{
		return true;
	}
	else
	{
		return false;
	}
}

should be like this
1
2
3
4
5
6
7
8
9
10
11
bool myName::operator>(myName m)
{
	if ( fn > m.ln )
	{
		return true;
	}
	else
	{
		return false;
	}
}
Last edited on
And the same with myFloat? Also, this is the only thread I've made on this, there is a similar one which only sorts the numbers for myFloat, not last names like in my case.

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
// ...
bool myFloat::operator>(myFloat m)
{
	if ( x > m.x )
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool myFloat::operator<(myFloat m)
{
	if ( x < m.x)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool myFloat::operator==(myFloat m)
{
	if ( x == m.x)
	{
		return true;
	}
	else
	{
		return false;
	}
}


Like this?

Also, I keep getting errors when I try to put my values for n1, n2, etc into the bubble sort and I also keep getting "no instance of function template bubblesort matches the argument list". And "no suitable conversion function from myFloat to float exists". This sort template is kicking my ass :(
Last edited on
Your original code works fine with one small change and adding the following three lines to main.cpp:

1
2
3
myName name_array [] = {n1, n2, n3, n4, n5 };
bubblesort(name_array,5);
for(int i = 0;i<5;++i) cout << name_array[i];


The change: In myname.cpp:line 43 you are comparing a first name to a last name.
Last edited on
Ok solved:)
For the template to work you need to use c style arrays
Meaning something like this:
1
2
myFloat flt[x]; // x number of items, or
myFloat *pFlt = new myFloat[x]; // x number of items 

It works the same for myName class;
I will post the whole code since the thing i copyied from here didn't compile due to some missing stuff :)
Compare with what you had and see what was wrong.
Also keep in mind that for strings it's better to use the string member function compare, rather the ">", "<", "==" signs :). I did that in the myName class;
check -> http://www.cplusplus.com/reference/string/string/compare/ for more info on string::compare;
I added a little bonus :P
main.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <iomanip>
#include "myFloat.h"
using namespace std;

template<class bubble>
void bubblesort(bubble a[], int n)
{
    int i, j;
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(a[i] > a[j])
            {
                bubble element;
                element = a[i];
                a[i] = a[j];
                a[j] = element;
            }
        }
    }
}

// i added another version of the bubblesort that i did, i think this one is more efficient :)
template<class bubble>
void myBubbleSort(bubble a[], int n)
{
    bool flag = 0;
    do
    {
        flag = 0;
        for(int i = 0; i < (n - 1); i++)
        {
            if(a[i] > a[i+1])
            {
                bubble elm;
                elm = a[i];
                a[i] = a[i + 1];
                a[i + 1] = elm;
                flag = 1;
            }
        }
    }while(flag);
}


int main()
{
	myFloat *flt = new myFloat[5];
	flt[0].setX(1.5);
	flt[0].setY(2.0);
	flt[1].setX(0.3);
	flt[1].setY(0.9);
	flt[2].setX(1.3);
	flt[2].setY(1.7);
	flt[3].setX(2.4);
	flt[3].setY(1.2);
	flt[4].setX(2.1);
	flt[4].setY(2.3);

	cout << "Before Sort\n";
	for(int i = 0; i < 5; i++)
    {
        cout << flt[i] << endl;
    }

	myName nme[5];
	nme[0].setF("Alex");
	nme[0].setL("Marian");
	nme[1].setF("Adrian");
	nme[1].setL("John");
	nme[2].setF("Justin");
	nme[2].setL("Angelo");
	nme[3].setF("Cata");
	nme[3].setL("Marcelo");
	nme[4].setF("Max");
	nme[4].setL("Adriano");

    bubblesort(flt, 5);
    myBubbleSort(nme, 5);
	cout << "\n";
	cout << "Before Sort\n";
	for(int i = 0; i < 5; i++)
    {
        cout << nme[i] << endl;
    }

	cout << "After Sort\n";

    for(int i = 0; i < 5; i++)
    {
        cout << flt[i] << endl;
    }

    for(int i = 0; i < 5; i++)
    {
        cout << nme[i] << endl;
    }
	return 0;
}

myFloat.h
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
#ifndef MYFLOAT_H
#define MYFLOAT_H
#include <iostream>
#include <string>
using namespace std;

class myFloat
{
	float x;
	float y;
public:
	myFloat();
	myFloat(float, float);
	void setX(float);
	void setY(float);
	float getX();
	float getY();
	ostream friend & operator<< (ostream &os, myFloat m);
	bool operator> (myFloat);
	bool operator< (myFloat);
	bool operator== (myFloat);
};

class myName
{
	string fn;
	string ln;
public:
	myName();
	myName(string, string);
	void setF(string);
	void setL(string);
	string getF();
	string getL();
	ostream friend & operator<< (ostream &, myName);
	bool operator> (myName);
	bool operator< (myName); // added this
	bool operator== (myName);// added this
};
#endif 

myFloat.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iomanip>
#include "myFloat.h"

myFloat::myFloat()
{
	x = 0.0;
	y = 0.0;
}

myFloat::myFloat(float a, float b)
{
	x = a;
	y = b;
}

void myFloat::setX(float a)
{
	x = a;
}

void myFloat::setY(float b)
{
	y = b;
}

float myFloat::getX()
{
	return x;
}

float myFloat::getY()
{
	return y;
}

ostream &operator << (ostream &os, myFloat m)
{
	os << " x = " << m.getX() << " y = " << m.getY() << endl;
	return os;
}

bool myFloat::operator>(myFloat m)
{
	if ( x > m.x )
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool myFloat::operator<(myFloat m)
{
	if ( x < m.x)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool myFloat::operator==(myFloat m)
{
	if ( x == m.x)
	{
		return true;
	}
	else
	{
		return false;
	}
}

myName.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iomanip>
#include "myFloat.h"

myName::myName()
{

}

myName::myName(string f , string l)
{
	fn = f;
	ln = l;
}

void myName::setF(string f)
{
	fn = f;
}

void myName::setL(string l)
{
	ln = l;
}

string myName::getF()
{
	return (fn);
}

string myName::getL()
{
	return (ln);
}

ostream & operator<< (ostream & create, myName obj)
{
	create << "Name is: " << obj.getF() << " " << obj.getL() << endl;
	return create;
}
// you use the compare function from std::string 
// because using just > < = to compare strings doesn't work so well
bool myName::operator>(myName m)
{
	if ( (fn.compare(m.fn) > 0))
	{
		return true;
	}
    else
	{
		return false;
	}
}
// you use the compare function from std::string
// because using just > < = to compare strings doesn't work so well
bool myName::operator<(myName m)
{
    if(fn.compare(m.fn) < 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
// added this
bool myName::operator==(myName m)
{
    // you use the compare function from std::string 
   // because using just > < = to compare strings doesn't work so well
    if(fn.compare(m.fn) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
Adding the do-while loop and using a bool in the bubble sort really cleared things up for me. I also changed things up a little bit since your code there compares the first names, I just modified it to compare and swap out the last names.

Thanks a lot nedo, you were a huge help. Kudos to you!
Glad to hear it. My pleasure .
Topic archived. No new replies allowed.