【发布时间】:2013-03-30 04:52:42
【问题描述】:
我无法让它工作,似乎无论我做什么它都无法正确排序。
我正在尝试根据点数按降序排序。
Bryan_Bickell 2 5 +2
Brandon_Bolig 0 3 0
Dave_Bolland 4 2 -1
Sheldon_Brookbank 0 4 -1
Daniel_Carcillo 0 1 +3
中间一栏是点数。
我正在使用 4 个数组来存储所有这些值,我将如何正确地利用数组选择排序使其以正确的方式排序?
我已经尝试了以下所有答案,但似乎没有一个有效,这就是我目前所拥有的
void sortArrays( string playerNames[], int goals[], int assists[], int rating[], int numPlayers )
{
int temp, imin;
int points[numPlayers];
for(int j = 0; j < numPlayers; j++)
{
points[j] = goals[j] + assists[j];
}
imin = points[0];
for(int i = 0; i < numPlayers; i++)
{
if (points[i] < imin)
{
imin = points[i];
}
}
for(int j = 1; j < numPlayers; j++)
{
if (points[j] > imin)
{
temp = points[j];
points[j] = points[j-1];
points[j-1] = temp;
}
}
}
【问题讨论】:
-
我猜你需要修改你的想法......选择排序处理在每次迭代中找到最小值......你没有设置任何最小值......请参考stackoverflow.com/questions/8362640/…
-
你的“交换”有问题;请注意,变量
temp从未设置过。此外,您的代码看起来像是冒泡排序的一部分,而不是选择排序。 -
这里有
std::swap;) ->using std::swap;...swap(points[i], points[i+1]); -
如果您有 4 个并行数组,则您的交换代码需要并行处理所有 4 个数组。使用一组结构会做得更好;大概,你还没有涵盖。
-
我想先对点进行排序,然后在函数关闭后处理所有 4 个数组将是一件轻而易举的事。
标签: c++ arrays algorithm sorting selection