随机排序可以实现吗?

qong 2004-10-09 09:40:31
现有一数组需将它随机排序,如何实现
...全文
169 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
IDqq 2004-10-09
  • 打赏
  • 举报
回复
100分就是由号召力啊,牛牛都大篇大篇的发表讲话咯……别跟我抢分我是一楼!
短歌如风 2004-10-09
  • 打赏
  • 举报
回复
是序列随机化还是用随机枢值快速排序?
unsigned int rand_by_range(unsigned int range)
{
return (double)rand() / RAND_MAX * range;
}
void swap(int * first, int * second)
{
int temp;
temp = * first;
* first = * second;
* second = temp;
}
序列随机化:
void random_shuffle(int * data, size_t count)
{
int i;
for(i = 0; i < count; i ++)
swap(data + i, data + rand_by_range(count));
}
随机枢值快速排序:
size_t partition(int * data, size_t count)
{
size_t m;
int value;
int uncomplete;
value := data[rand_by_range(count --)];
m = 0;
uncomplete = count > m;
while (uncomplete)
{
while (data[m] < value)
++ m;
while (value < data[count])
-- count;
uncomplete = count > m;
if (uncomplete)
swap(data + m ++, data + count --);
}
}

void quick_sort(int * data, size_t count)
{
if (count > 0)
{
int m;
m = partition(data, count);
quick_sort(data, m);
quick_sort(data + m + 1, count - m - 1);
}
}
daylove 2004-10-09
  • 打赏
  • 举报
回复
void
random_shuffle (RandomAccessIterator beg, RandomAccessIterator end)
void
random_shuffle (RandomAccessIterator beg, RandomAccessIterator end,
RandomFunc& op)

• The first form shuffles the order of the elements in the range [beg,end) using a uniform
distribution random number generator.
• The second form shuffles the order of the elements in the range [beg,end) using op. op is
called with an integral value of difference_type of the iterator:
op (max)
It should return a random number greater than or equal to zero and less than max. Thus,
it should not return max itself.
• Note that op is a nonconstant reference, so you can't pass a temporary value or an
ordinary function.
• Complexity: linear (numberOfElements-1 swaps).
The C++ Standard Library
dyne-book 341
You might wonder why random_shuffle() uses its optional operation as a nonconstant
reference. It does so because random number generators typically have a local state. Old global
C functions such as rand() store their local state in a static variable. However, this has some
disadvantages: For example, the random number generator is inherently thread unsafe, and you
can't have two independent streams of random numbers. Therefore, function objects provide a
better solution by encapsulating their local state as one or more member variables. Thus, the
random number generator can't be constant because it has to change its local state while
generating a new random number. However, to have the random number generator nonconstant,
you could still pass it by value instead of passing it by nonconstant reference. In this case each
call would copy the random number generator and its state so that you get the same random
sequence each time you pass the generator to the algorithm. Thus the generator is passed as a
nonconstant reference. [6]
[6] Thanks to Matt Austern for this explanation.
If you need the same random number sequence twice, you can simply copy it. However, if the
generator is implemented in a way that uses a global state, you would still get different
sequences.
The following example demonstrates how to shuffle elements by calling random_shuffle():

// algo/random1.cpp
#include <cstdlib>
#include "algostuff.hpp"
using namespace std;

c1ass MyRandom {
public:
ptrdiff_t operator() (ptrdiff_t max) {
double tmp;
tmp = static_cast<double>(rand())/ static_cast<double>(RAND_MAX);
return static_cast<ptrdiff_t>(tmp * max);
}
};

int Main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,1,9);
RINT_ELEMENTS(coll,"coll: ");
// shuffle all elements randomly
random_shuffle (coll.begin(), coll.end());
PRINT_ELEMENTS(coll."shuffled: ");
// sort them again
sort (coll.begin(), coll.end());
PRINT_ELEMENTS(coll,"sorted: ");

/*shuffle elements with self-written random number generator
*-to pass an lvalue we have to use a temporary object
*/

MyRandom rd;
random_shuffle (coll.begin(), coll.end(), rd); // random number generator
PRINT_ELEMENTS(coll,"shuffled: ");
}

The second call of random() uses the self-written random number generator rd(). It is an
object of the auxiliary function object c1ass MyRandom, which uses an algorithm for random
numbers that often is better than the usual direct call of rand().[7]
[7] The way MyRandom generates random numbers is introduced and described in Bjarne Stroustrup's The
C++ Programming Language, 3rd edition.
A possible (but not portable) output of the program is as follows:
coll: 1 2 3 4 5 6 7 8 9
shuffled: 2 6 9 5 4 3 1 7 8
sorted: 1 2 3 4 5 6 7 8 9
shuffled: 2 6 9 3 1 8 7 4 5
  • 打赏
  • 举报
回复
同意
IDqq 2004-10-09
  • 打赏
  • 举报
回复
STL random_shuff

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧