数组随机排序

相比数组排序使用频率而言,数组随机排序使用的貌似没有那么多,但有时候也是必须的,下面是利用随机数实现的简单的随机排序。

template <class T>
void sortRandom(vector<T> &vec)
{
    srand((unsigned int)time(NULL));
    size_t size = vec.size();
    for (int i = 0; i<size; i++)
    {
        int r = rand() % size;
        swap(vec[i], vec[r]);
    }
}
posted @ 2015-10-19 16:57  随风的博客  阅读(76)  评论(0编辑  收藏  举报