Advertisement

sorting algorithm

Started by December 28, 2024 10:54 PM
15 comments, last by pbivens67 1 day, 8 hours ago

I am working on a sorting program that sorts batting averages and names, and prints them to the screen. I am trying to get the sortData function to work in the main function

#include <iostream>
#include <string>

using namespace std;

class Average
{
private:
	string name[12];
	double avg[12];
public:
	void getData();
	void sortData(string[], double[], int);
	void displayData();
};
void Average::getData()
{
	string play_name;
	double average;
	for (int i = 0; i < 12; i++)
	{
	cout << "Enter name: ";
	cin >> play_name;
	cout << endl;
	cout << "Enter average: ";
	cin >> average;
	cout << endl;
	name[i] = play_name;
	avg[i] = average;
	}
}
void Average::sortData(string name[], double avg[],int size)
{
	int temp;
	bool madeAswap = false;
	do
	{
		madeAswap = false;
		for (int count = 0; count < size-1; count++)
		{
			if (avg[count] > avg[count + 1])
			{
				temp = avg[count];
				avg[count] = avg[count + 1];
				avg[count + 1] = temp;
				madeAswap = true;
			}
		}
	} while (madeAswap);

//	int temp;
//	bool madeAswap = false;
	do
	{
		madeAswap = false;
		for (int count = 0; count < size-1; count++)
		{
			if (name[count] > name[count + 1])
			{
				string temp = name[count];
				name[count] = name[count + 1];
				name[count + 1] = temp;
				madeAswap = true;
			}
		}
	} while (madeAswap);
}
void Average::displayData()
{
	for (int i = 0; i < 12; i++)
	{
		cout << name[i] << " " << avg[i] << endl;
	}
}
int main()
{
	Average avg;
	avg.getData();
//	avg.sortData();
	avg.displayData();
	return 0;
}

Generally, you would use the std::sort function, by overloading the < operator. Please consider the following code:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


class name_avg_pair
{
public:
	string name;
	double avg;

	bool operator<(const name_avg_pair& rhs) const
	{
		if (name < rhs.name)
			return true;
		else if (name > rhs.name)
			return false;

		if (avg < rhs.avg)
			return true;
		else if (avg > rhs.avg)
			return false;

		return false;
	}
};


class Average
{
private:
	vector<name_avg_pair> pair_data;

public:
	void get_data(void)
	{
		pair_data.resize(12);

		pair_data[0].name = "Bob";
		pair_data[0].avg = 0.95;

		pair_data[1].name = "Alice";
		pair_data[1].avg = 0.1234;

		pair_data[2].name = "Cammy";
		pair_data[2].avg = 0.8;

		pair_data[3].name = "Bob";
		pair_data[3].avg = 0.4567;

		// ...
	}

	void sort_data(void)
	{
		sort(pair_data.begin(), pair_data.end());
	}

	void display_data(void)
	{
		for (size_t i = 0; i < pair_data.size(); i++)
			if(pair_data[i].name != "" && pair_data[i].avg != 0.0)
				cout << pair_data[i].name << " " << pair_data[i].avg << endl;
	}
};


int main(void)
{
	Average a;

	a.get_data();
	a.sort_data();
	a.display_data();

	return 0;
}
Advertisement

is there a way to code this without using the vector or algorithm header files

You can use an array instead of a vector, but <algorithm> is needed for the sort function.

#include <iostream>
#include <algorithm>
using namespace std;


const unsigned int num_pairs = 12;


class name_avg_pair
{
public:
	string name;
	double avg;

	bool operator<(const name_avg_pair& rhs) const
	{
		if (name < rhs.name)
			return true;
		else if (name > rhs.name)
			return false;

		if (avg < rhs.avg)
			return true;
		else if (avg > rhs.avg)
			return false;

		return false;
	}
};


class Average
{
private:
	name_avg_pair pair_data[num_pairs];

public:
	void get_data(void)
	{
		pair_data[0].name = "Bob";
		pair_data[0].avg = 0.95;

		pair_data[1].name = "Alice";
		pair_data[1].avg = 0.1234;

		pair_data[2].name = "Cammy";
		pair_data[2].avg = 0.8;

		pair_data[3].name = "Bob";
		pair_data[3].avg = 0.4567;

		// ...
	}

	void sort_data(void)
	{
		sort(pair_data, pair_data + num_pairs);
	}

	void display_data(void)
	{
		for (size_t i = 0; i < num_pairs; i++)
			if(pair_data[i].name != "" && pair_data[i].avg != 0.0)
				cout << pair_data[i].name << " " << pair_data[i].avg << endl;
	}
};


int main(void)
{
	Average a;

	a.get_data();
	a.sort_data();
	a.display_data();

	return 0;
}

What exactly is your question? How to call the functions?

For one, you already store the data in your class, so sortData() should have no parameters. The data is not external but already known and stored in the class.

Also you are storing the data private, so you need PUBLIC accessors:

string* GetNames(){ return names;}
double* GetAverages(){ return averages:}

sortData(GetNames(), getAverages(), 12);

NBA2K, Madden, Maneater, Killing Floor, Sims

…. and before you discount the std::sort function, do realize that it — especially the overloading of the less-than operator — has been on 100% of the C++ tests that I’ve been subjected to. I can’t say where, but it’s true nonetheless.

Advertisement

is there anyway I can code this only using the bubble sort? here is my updated code

#include <iostream>
#include <string>

using namespace std;

class Average
{
private:
	string name[12];
	double avg[12];
public:
	void getData();
	void sortData();
	void displayData();
};
void Average::getData()
{
	string play_name;
	double average;
	for (int i = 0; i < 12; i++)
	{
	cout << "Enter name: ";
	cin >> play_name;
	cout << endl;
	cout << "Enter average: ";
	cin >> average;
	cout << endl;
	name[i] = play_name;
	avg[i] = average;
	}
}
void Average::sortData()
{
	int temp;
	bool madeAswap = false;
	do
	{
		madeAswap = false;
		for (int count = 0; count < 11; count++)
		{
			if (avg[count] > avg[count + 1])
			{
				temp = avg[count];
				avg[count] = avg[count + 1];
				avg[count + 1] = temp;
				madeAswap = true;
			}
		}
	} while (madeAswap);

//	int temp;
//	bool madeAswap = false;
	do
	{
		madeAswap = false;
		for (int count = 0; count < 11; count++)
		{
			if (name[count] > name[count + 1])
			{
				string temp = name[count];
				name[count] = name[count + 1];
				name[count + 1] = temp;
				madeAswap = true;
			}
		}
	} while (madeAswap);
}
void Average::displayData()
{
	for (int i = 0; i < 12; i++)
	{
		cout << name[i] << " " << avg[i] << endl;
	}
}
int main()
{
	Average avg;
	avg.getData();
	avg.sortData();
	avg.displayData();
	return 0;
}

pbivens67 said:
is there anyway I can code this only using the bubble sort? here is my updated code

Why do you have a loop that separately sorts the names, and a loop that separately sorts the averages?

Obviously both get sorted, but the relation between avg[j] and name[j] is then lost.

If you want to keep that relation, you have to swap elements in both arrays in the same way (as in, if you swap avg[5] with avg[7], you must then also swap name[5] with name[7]).

The better tactic here is to make a class that contains both the name and the average, like @taby did. Then you can swap two avg_name_pairs with each other. Since that class contains both the name and the average, you thus swap both values at the same time.

Yes, in case it's not clear to you, you still need to overload the < operator, and then use that in the bubble sort, instead of the > operator.

Advertisement