【发布时间】:2016-01-09 04:21:03
【问题描述】:
我正在修补 C++,我喜欢 let's make a sorting algorithm :)
我做了一些事情,但它没有对数组进行排序,而是用最大数覆盖了数组
我不知道我的错误在哪里,因为我在纸上运行了算法(不要问)并且它是正确的。
我尝试了所有可能的修改。
有什么帮助吗?
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int mn = 0, mx = 0;
int a[] = { 4, 8, 5, 2, 6, 9, 0, 3, 1, 7 };
for (int i = 0; i < 10; i++)
{
mn = a[i]; mx = a[i];
for (int j = i; j < 10 - i; j++)
{
mn = min(a[j], mn);
mx = max(a[j], mx);
}
swap(a[i], mn);
swap(a[10-1-i], mx);
}
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
}
【问题讨论】:
标签: c++ arrays algorithm sorting overwrite