-1
我想要做的是實現一個簡單的選擇排序算法,該算法使用函數minButGreaterThan來查找數組中的下一個最小數字。我的問題是,如果數組有一個重複的號碼,它會被傳遞並留在最後。我試着改變控制if語句來適應這個,但似乎沒有任何工作。有什麼建議?選擇排序不會捕獲重複的數字
double GradeBook::minButGreaterThan(double x) // - NEEDS TESTING
{
double minButGreaterThan = -1;
for (int i = 0; i < classSize; i++)
{
if (grades[i] > x)
{
minButGreaterThan = grades[i];
break;
}
}
for (int i = 0; i < classSize; i++)
{
if (grades[i] > x && grades[i] <= minButGreaterThan)
minButGreaterThan = grades[i];
}
return minButGreaterThan;
}
void GradeBook::selectionSort() //ascending order -- *DOES NOT WORK WITH DUPLICATE SCORES* - RETEST
{
double min = absoluteMin();
for (int i = 0; i < classSize; i++)
{
if (grades[i] == min)
{
double temp = grades[0];
grades[0] = grades[i];
grades[i] = temp;
break;
}
}
for (int i = 0; i < classSize-1; i++)
{
double next = minButGreaterThan(grades[i]);
for (int n = 1; n <= classSize; n++)
if (grades[n] == next)
{
double temp = grades[n];
grades[n] = grades[i+1];
grades[i+1] = temp;
}
}
}
這可能適合codereview.stackexchange.com –