1 Replies - 1664 Views - Last Post: 23 January 2013 - 08:38 PM Rate Topic: -----

#1 tarantino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-January 13

selection sort on an array of class objects incomplete

Posted 23 January 2013 - 07:36 PM

i have written a selection sort algorithm to go sort an array of class objects by age in ascending order, the problem is that the output being given does not match what i think the code should do. when the program runs the 3 records are added to the array and when they are sorted should be outputed in ascending order, the problem is that with my code the last 2 are sorted properly but the first element does not seem to move, it remains the same as the original unsorted value.

thanks for any help given.

my code for the class, selection sort function and the display method are below:


void selectionSort()
{
	int i, minIndex, minValue;
	for (i = 0; i < (arrlength - 1); i++)
	{
		minIndex = i ;
		minValue = player[i].getAge() ;
		for (int index = i + 1; index < arrlength; index++)
		{
			if (player[index].getAge() < minValue)
			{
				minValue = player[index].getAge();
				minIndex = index;

			}
		}

		player[minIndex].setAge(player[i].getAge());
		player[i].getAge() == minValue;

	}



}





void displayallinfo()
{

	selectionSort();

	for (int i=0; i < 3; i++)
	{
		cout << "\n First Name : " << player[i].getFirstName() << "\n" << "Last Name : " << player[i].getLastName() <<
			"\n" << "Age : " << player[i].getAge() << "\n" << "Current Team : " << player[i].getCurrentTeam() << 
			"\n" << "Position : " << player[i].getPosition() << "\n" << "Status :  " << player[i].getStatus()  << "\n\n";
	}

	cin.get() ;

	menu() ;
}







class user 
{
	string firstname, lastname, currentteam, position, status ;
	int age ;
public:
	user() {};
	user(string fname, string lname, string cteam, string pos, string stat, int age) 
	{
		setFirstName(fname);
		setLastName(lname);
		setCurrentTeam(cteam);
		setPosition(pos);
		setStatus(stat);
		setAge(age);
	} ;



	user& operator = (const user& source)
	{
		firstname = source.firstname;
		lastname = source.lastname ;
		currentteam = source.currentteam ;
		position = source.position ;
		status = source.status ;
		age = source.age ;
	}


	void setFirstName(string fname)
		{firstname = fname;}
	void setLastName(string lname)
		{lastname = lname;}
	void setCurrentTeam(string cteam)
		{currentteam = cteam;}
	void setPosition(string pos)
		{position = pos;}
	void setStatus(string stat)
		{status = stat;}
	void setAge(int _age)
		{age = _age;}

	string getFirstName()
		{return firstname ;}
	string getLastName()
		{return lastname ;}
	string getCurrentTeam()
		{return currentteam ;}
	string getPosition()
		{return position ;}
	string getStatus()
		{return status ;}
	int getAge()
		{return age ;}
};





Is This A Good Question/Topic? 0
  • +

Replies To: selection sort on an array of class objects incomplete

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,320
  • Joined: 27-December 08

Re: selection sort on an array of class objects incomplete

Posted 23 January 2013 - 08:38 PM

You don't want to swap the Players' ages. Swap the Player objects instead. Here, your code doesn't make sense. Especially the last line:
player[minIndex].setAge(player[i].getAge());
player[i].getAge() == minValue;



Swap player[minIndex] and player[i] instead.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1