如何根据双指针所指向的值对数组进行排序?
时间:2020-03-05 18:40:58 来源:igfitidea点击:
我正在尝试在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. } } } }
解决方案
回答
你近了交换时引用的是数组项的地址,这不是必需的。数组中的项目是指针,这就是需要交换的内容。
见下文:
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. } } } }
另外,如果我们有兴趣,请查看这篇有关冒泡排序的可爱博客文章(对不起,无耻的插件:))。希望对家庭作业有所帮助;)
编辑:请注意微妙的"优化",我们可以从数组长度开始倒数,并且仅递增直到内部循环中的" i"为止。这样可以避免不必要地重新分析已排序的项目。
回答
Heh, this isnt homework.
如果是这样,请考虑使用STL来管理数组和排序。它更易于开发和维护,并且std :: sort算法比气泡排序渐近地快。
回答
我们应该考虑使用std :: swap()
进行交换。如果这样做,则这样称呼它:
swap( obj1, obj2 );
而不是:
std::swap( obj1, obj2 );
作为第一个调用语义,如果存在的话,将允许适当的名称空间查找来找到正确的重载。确保具有以下任一条件:
using namespace std;
或者:
using std::swap;
某处。
回答
Hmm, I don't have much experience with the STL. Could you give an example?
该程序创建一个整数向量,对其进行排序并显示结果。
#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; } }
回答
要完成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的<algorithm>标头看起来像http://www.cplusplus.com/reference/algorithm/
我们会发现很多实用程序。请注意,我们还有其他可能更适合容器实现(std :: list?std :: map?)。