Advertisement

sorting algorithm

Started by December 28, 2024 10:54 PM
16 comments, last by dpadam450 1 week ago

I compacted my code, is this better?

#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()
{
	bool madeAswap = false;
	do
	{
		madeAswap = false;
		for (int count = 0; count < 11; count++)
		{
			if (avg[count] > avg[count + 1])
			{
				int temp = avg[count];
				avg[count] = avg[count + 1];
				avg[count + 1] = temp;
				madeAswap = true;

			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;
}
#include <iostream>
#include <utility>
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)
	{
		// Bubble sort
		for (size_t i = 0; i < num_pairs - 1; i++)
			for (size_t j = 0; j < num_pairs - i - 1; j++)
				if (pair_data[j + 1] < pair_data[j])
					swap(pair_data[j], pair_data[j + 1]);
	}

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

I need opinions not just code I cant learn anything with just code

The only important part of the code is that I used the < operator to compare the average-name pairs.

There will come a day when you will be able to read code and get the idea, faster than in English. I believe in you.

I figured out my problem it just takes a little practice

Advertisement

If this is for educational purposes, you don't want to copy strings repeatedly while sorting.
create an array of -1 integers. Each time you copy write the indices you are copying, then do one final copy of all the names later.

0,1,2,3,4,5 → 0,1,2,3,4,5 ( already sorted)
-1,-1,-1,-1,-1 (if -1, then name didnt move no need to copy it

5,4,3,2,1 → 0,1,2,3,4,5

-1,-1,-1,-1,-1 becomes 5,4,3,2,1 (the index the numbers moved to) so copy index[0] to position 5, index[1] to position 4 etc.

NBA2K, Madden, Maneater, Killing Floor, Sims

Advertisement