2008-08-20 62 views
2

我想在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. 
     } 
    } 
    } 
} 
+0

另請參閱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

回答

5

你很近。您在交換時引用了數組項的地址,這不是必需的。數組中的項是指針,這就是需要交換的內容。

見下文:

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 = ArrayLength - 1; i > 0 && flag; i--) 
    { 
     flag = 0; 
     for (j = 0; j < i; j++) 
     { 
      if (*pArray[j] > *pArray[j+1])  // 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. 
      } 
     } 
    } 
} 

此外,檢查出this lovely blog post on Bubble Sorting的情況下,你有興趣(對不起,無恥插頭:))。希望可以幫助你做作業;)


編輯:請注意,你指望從數組長度後面只有等到「我」在內環增加了微妙的「優化」。這可以避免不必要地重新分析已排序的項目。

3

嘿,這不是作業。

如果是這種情況,那麼考慮使用STL來管理數組和排序。它更容易開發和維護,而std :: sort算法比氣泡排序漸近地快。

2

您應該考慮使用std::swap()進行交換。如果你這樣做,把它作爲這樣的:

swap(obj1, obj2); 

而不是:

std::swap(obj1, obj2); 

作爲第一通話語義將允許適當的名稱空間查詢,找到正確的過載(如果存在)。一定要有兩種:

using namespace std; 

或:

using std::swap; 

地方。

1

嗯,我沒有太多的STL經驗。你能舉個例子嗎?

該程序創建一個整數的向量,對它進行排序並顯示結果。

#include <vector> 
#include <algorithm> 
#include <iostream> 
using namespace std; 

int main() 
{ 
    vector<int>; vec; 
    vec.push_back(7); 
    vec.push_back(5); 
    vec.push_back(13); 
    sort(vec.begin(), vec.end()); 

    for (vector<int>::size_type i = 0; i < vec.size(); ++i) 
    { 
     cout << vec[i] << endl; 
    } 
} 
0

要完成Brian Ensink的文章,您會發現STL充滿驚喜。例如,std :: sort算法:

#include <iostream> 
#include <vector> 
#include <algorithm> 

void printArray(const std::vector<int *> & p_aInt) 
{ 
    for(std::vector<int *>::size_type i = 0, iMax = p_aInt.size(); i < iMax; ++i) 
    { 
     std::cout << "i[" << static_cast<int>(i) << "] = " << reinterpret_cast<unsigned  int>(p_aInt[i]) << std::endl ; 
    } 

    std::cout << std::endl ; 
} 


int main(int argc, char **argv) 
{ 
    int a = 1 ; 
    int b = 2 ; 
    int c = 3 ; 
    int d = 4 ; 
    int e = 5 ; 

    std::vector<int *> aInt ; 

    // We fill the vector with variables in an unordered way 
    aInt.push_back(&c) ; 
    aInt.push_back(&b) ; 
    aInt.push_back(&e) ; 
    aInt.push_back(&d) ; 
    aInt.push_back(&a) ; 

    printArray(aInt) ; // We see the addresses are NOT ordered 
    std::sort(aInt.begin(), aInt.end()) ; // DO THE SORTING 
    printArray(aInt) ; // We see the addresses are ORDERED 

    return EXIT_SUCCESS; 
} 

第一次打印數組將顯示無序地址。第二,排序後,將顯示有序的地址。在我的編譯器,我們有:

i[0] = 3216087168 
i[1] = 3216087172 
i[2] = 3216087160 
i[3] = 3216087164 
i[4] = 3216087176 

i[0] = 3216087160 
i[1] = 3216087164 
i[2] = 3216087168 
i[3] = 3216087172 
i[4] = 3216087176 

給STL的<算法>頭一看http://www.cplusplus.com/reference/algorithm/ 你會發現很多的工具。請注意,您還有其他可以更適合您的容器的實現(std :: list?std :: map?)。