您的位置:首页 > 其它

排序1,2......n的无序数组,时间复杂度为o(n),空间复杂度为o(1)

2016-04-26 15:56 393 查看
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a[] = { 10, 6, 9, 5, 2, 8, 4, 7, 1, 3 };
int len = sizeof(a) / sizeof(int);
int temp;
for (int i = 0; i < len;)
{
temp = a[a[i] - 1];
a[a[i] - 1] = a[i];
a[i] = temp;
//当数字的大小等于位置大小的时候,说明该数字移到了正确位置
if (a[i] == i+1)
{
i++;
}
}
for (int j = 0; j < len; j++)
{
cout << a[j] << " ";
}
cout << endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: