我想在C/C++中構建一個函數來對數組進行排序,並用它的「分數」或等級替換每個值。它接受一個雙精度指針數組作爲整數,並根據整數的解引用值對雙精度指針進行排序。我已經嘗試了很多次,以使其工作,但無法解決它。再次,它必須根據它們指向的值對雙指針進行排序。這是我有:如何根據指向的值對雙指針數組進行排序?
void SortArray(int ** pArray, int ArrayLength)
{
int i, j, flag = 1; // set flag to 1 to begin initial pass
int * temp; // holding variable orig with no *
for(i = 1; (i <= ArrayLength) && flag; i++)
{
flag = 0;
for (j = 0; j < (ArrayLength -1); j++)
{
if (*pArray[j+1] > *pArray[j]) // ascending order simply changes to <
{
temp = &pArray[j]; // swap elements
pArray[j] = &pArray[j+1];
pArray[j+1] = &temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
另請參閱http://stackoverflow.com/questions/5632832/sort-an-array-based-on-an-index-array-in-c其中我給出了兩個例子。使用O(log(n))而不是O(N^2) – elcuco 2013-01-01 09:56:56