selection sort on an array of class objects

hello everyone

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 selection sort function and the display method are below:

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

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;

	}



}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

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() ;
}
1
2
player[minIndex].setAge(player[i].getAge()); //¿eh?
player[i].getAge() == minValue; //statement has no effect 
You should swap the `current' object with the minimum object.


Also, it seems that you don't understand code flow, as you call `menu()' inside `displayallinfo()'
Topic archived. No new replies allowed.