【发布时间】:2010-09-06 00:32:00
【问题描述】:
我想使用C/C++ 对数组进行升序排序。结果是一个包含元素索引的数组。每个索引都对应于排序数组中的元素位置。
示例
Input: 1, 3, 4, 9, 6
Output: 1, 2, 3, 5, 4
编辑: 我正在使用 shell 排序程序。重复值索引是根据原始数组中首先出现的重复值来任意选择的。
更新:
尽管我尽了最大努力,但我仍然无法为指针数组实现排序算法。当前示例无法编译。
谁能告诉我怎么了?
非常感谢您的帮助!
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]; //the problem lies somewhere in here
&pArray[j + 1] = &temp;
flag = 1; // indicates that a swap occurred.
}
}
}
};
【问题讨论】: