如何在没有随机性的情况下对列表进行洗牌,并保证一部分元素最终会出现在一侧?
给定一个元素列表,是否存在一种洗牌算法可以保证最终选定的一半部分位于一侧,其余部分位于另一侧?
例子: { 4, 3, 10, 7, 2, 9, 6, 8, 1, 5 }
鉴于上面的集合,我希望有一个混合算法,最终将标记的向左移动,即使算法本身不知道什么是“标记”和什么不是“标记”。
{ 4, 3, 10, 7, 2, 9, 6, 8, 1, 5 }
X X X X X
可接受的结果是:
{ 4, 10, 9, 6, 1, 3, 7, 2, 8, 5 }
{ 1, 9, 10, 4, 6, 2, 8, 5, 7, 3 }
{ 1, 4, 9, 10, 6, 3, 7, 5, 8, 2 } 等
难度: 算法不应该使用随机数来混合内容,应该是一个迭代的过程。 所以 Fisher-Yates 已经出局了。
Given a list of elements, does a shuffling algorithm exist that will guarantee that eventually a selected half portion will be on one side, and the remainder on the other?
Example:
{ 4, 3, 10, 7, 2, 9, 6, 8, 1, 5 }
Given the set above, I'd like to have a mixing algorithm that eventually moves the marked ones to the left, even though the algorithm itself has no idea what is and isn't "marked."
{ 4, 3, 10, 7, 2, 9, 6, 8, 1, 5 }
X X X X X
Acceptable results would be:
{ 4, 10, 9, 6, 1, 3, 7, 2, 8, 5 }
{ 1, 9, 10, 4, 6, 2, 8, 5, 7, 3 }
{ 1, 4, 9, 10, 6, 3, 7, 5, 8, 2 } etc
Difficulty: The algorithm shouldn't use random numbers to mix the contents, it should be an iterative process. So Fisher-Yates is out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的列表分成两个列表 - 带标记的列表和未标记的列表。打乱两个子列表,并将已标记的子列表放在一侧,将未标记的子列表放在另一侧。现在您可以使用任何您想要的洗牌算法。
Split your list into two lists - flagged and unflagged. Shuffle both sublists, and put the flagged on one side and the unflagged on the other. Now you can use any shuffling algo you want.
std::next_permutation()
会是你的吗想? (由于它创建了所有可能的排列,因此最终也会将标记一次放在左侧。)Would
std::next_permutation()
be what you want? (Since it creates all possible permutations, it will, eventually, also put the marked once to the left.)像
next_permutation
这样的东西可以完成这项工作,并且(最终)像 bogo sort 这样的东西也可以完成这项工作。两者最终都会产生所有可能的排列,包括您认为可接受的所有排列(以及一些您不可接受的任意数字)。Bogo 排序表明您的:“算法不应该使用随机数来混合内容,它应该是一个迭代过程。所以 Fisher-Yates 已经出局了。”是一个错误的二分法。 Bogo 排序随机混合并且它是迭代的。
Something like
next_permutation
will do the job, and (eventually) so will something like bogo sort. Either will eventually produce every possible permutation, including all the permutations you consider acceptable (along with some arbitrary number you don't).Bogo sort shows that your: "The algorithm shouldn't use random numbers to mix the contents, it should be an iterative process. So Fisher-Yates is out." is a false dichotomy. Bogo sort mixes randomly and it's iterative.
我认为您正在寻找一种算法:
* 可用于向用户显示看起来像洗牌的迭代过程
* 最终将原始集合分为 2 个(预选)组,但在每个组中随机排序
在我看来,简单的东西可能适合您考虑您的十个数字并使用该算法标记/未标记的组的术语:
这不不像 Fisher-Yates 那样提供有效的、统计上良好的洗牌,但确实可以在屏幕上提供美观的混合效果。
I think you are looking for an algorithm that:
* Can be used to display a iterative process to a user that looks something like shuffling
* Ends up with the original set separated into 2 (preselected) groups but randomly ordered within each group
Seems to me that something simple might suit you well consider for your ten numbers and using the terminology marked/unmarked for the groups this algorithm:
This doesn't give an efficient statistically good shuffle like Fisher-Yates but does give you a good looking on screen mix up.