我無法得到這個工作,似乎無論我做什麼,它從來沒有正確排序。如何正確使用選擇排序算法對列表進行排序?
我想根據點數降序排序。
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;
}
}
}
我猜你需要修改你個優化上墨...選擇排序處理在每次迭代中尋找最小值...您沒有設置任何最小值... pls參考http://stackoverflow.com/questions/8362640/java-selection-sort-algorithm?rq = 1 –
你的「交換」是越野車;注意變量'temp'從不設置。此外,你的代碼看起來像是冒泡排序的一部分,而不是選擇排序。 – comingstorm
這裏有'std :: swap';) - >'使用std :: swap;'''swap(points [i],points [i + 1]);' – leemes