我正在尝试对数组进行从小到大的排序,但我真的迷路了....
这是我目前所拥有的:
int temp, temp2;
for (int x = 0; x < array_size; x++)
{
temp=a[x];
for (int i = 0; i < array_size; i++)
{
if (a[i] < temp)
{
temp2=a[i];
a[i]=temp;
a[x]=temp2;
}
}
}
更新:仍然无法正常工作,我必须使用代码。
int temp, temp2, x=-1;
for (int x = 0; x < array_size; x++)
{
temp=a[x];
for (int i = x+1; i < array_size; i++)
{
if (a[i] < temp)
{
temp2=a[i];
a[i]=temp;
a[x]=temp2;
}
}
}
最佳答案
除非这是家庭作业并且您可以使用的功能受到限制:
#include <algorithm>
...
std::sort(a,a+array_size);
关于c++ - 尝试对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3983541/