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

#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;
}
原文地址:https://www.cnblogs.com/pengjun-shanghai/p/5435423.html