本文介绍了什么是最快的方式,以7整数数组排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个程序,分析了扑克的可能性,特别是德州扑克的一部分。我有一个程序我很高兴,但它需要一些小的优化是完美的。

This is a part of a program that analyzes the odds of poker, specifically Texas Hold'em. I have a program I'm happy with, but it needs some small optimizations to be perfect.

我用这个类型(除其他外,当然):

I use this type (among others, of course):

  type
    T7Cards = array[0..6] of integer;

有两件事情有关此阵作出决定时,可能是重要的如何排序的:

There are two things about this array that may be important when deciding how to sort it:

  1. 在每一个项目是一个值从0到51没有其他值是可能的。
  2. 有没有重复。从来没有。

通过这些信息,什么是绝对是最快的方式来这个数组排序?我使用Delphi,所以帕斯卡code将是最好的,但我可以读C和伪,虽然有点更慢: - )

With this information, what is the absolutely fastest way to sort this array? I use Delp so pascal code would be the best, but I can read C and pseudo, albeit a bit more slowly :-)

目前我使用的快速排序的瞬间,但有趣的是,这几乎不会高于冒泡!可能因为少数项目。分拣计数的方法的总运行时间近50%的

At the moment I use quicksort, but the funny thing is that this is almost no faster than bubblesort! Possible because of the small number of items. The sorting counts for almost 50% of the total running time of the method.

编辑:

梅森惠勒问为什么有必要进行优化。一个原因是,该方法将被调用2118760次

Mason Wheeler asked why it's necessary to optimize. One reason is that the method will be called 2118760 times.

基本扑克信息:所有玩家发两张牌(口袋),然后五张牌牌给表(3首被称为翻牌,接下来就是转,最后是河流每个球员选秀权。五个最好的卡来弥补自己的手)

Basic poker information: All players are dealt two cards (the pocket) and then five cards are dealt to the table (the 3 first are called the flop, the next is the turn and the last is the river. Each player picks the five best cards to make up their hand)

如果我在口袋有两个卡,P1和P2,我将使用下面的循环,以产生所有可能的组合:

If I have two cards in the pocket, P1 and P2, I will use the following loops to generate all possible combinations:

for C1 := 0 to 51-4 do
  if (C1<>P1) and (C1<>P2) then
     for C2 := C1+1 to 51-3 do
       if (C2<>P1) and (C2<>P2) then
         for C3 := C2+1 to 51-2 do
           if (C3<>P1) and (C3<>P2) then
             for C4 := C3+1 to 51-1 do
               if (C4<>P1) and (C4<>P2) then
                 for C5 := C4+1 to 51 do
                   if (C5<>P1) and (C5<>P2) then
                   begin
                     //This code will be executed 2 118 760 times
                     inc(ComboCounter[GetComboFromCards([P1,P2,C1,C2,C3,C4,C5])]);
                   end;

在我写这篇文章,我注意到一件事:数组的最后五元素将始终进行排序,所以它把前两个元素的数组在合适的位置只是一个问题。这应该简化问题了一下。

As I write this I notice one thing more: The last five elements of the array will always be sorted, so it's just a question of putting the first two elements in the right position in the array. That should simplify matters a bit.

那么,新的问题是:什么是最快的方式,以7整数数组排序,当最后5个元素已经排序。 (?)我相信这可能是一对夫妇的解决,如果公司和掉期交易: - )

So, the new question is: What is the fastest possible way to sort an array of 7 integers when the last 5 elements are already sorted. I believe this could be solved with a couple (?) of if's and swaps :-)

推荐答案

我不知道很多关于德州扑克:它是否不管什么衣服P1和P2,或者它只是如果他们的事花色相同或不?如果只服(P1)==套装(P2)的问题,那么你可以区分这两种情况下,你只有对P1 / P2 13x12 / 2不同的可能性,您可以轻松地precalculate一个表的两种情况。

I don't know that much about Texas Hold'em: Does it matter what suit P1 and P2 are, or does it only matter if they are of the same suit or not? If only suit(P1)==suit(P2) matters, then you could separate the two cases, you have only 13x12/2 different possibilities for P1/P2, and you can easily precalculate a table for the two cases.

另外,我建议是这样的:

Otherwise, I would suggest something like this:

(* C1 < C2 < P1 *)
for C1:=0 to P1-2 do
   for C2:=C1+1 to P1-1 do
      Cards[0] = C1;
      Cards[1] = C2;
      Cards[2] = P1;
      (* generate C3...C7 *)

(* C1 < P1 < C2 *)
for C1:=0 to P1-1 do
   for C2:=P1+1 to 51 do
      Cards[0] = C1;
      Cards[1] = P1;
      Cards[2] = C2;
      (* generate C3...C7 *)

(* P1 < C1 < C2 *)
for C1:=P1+1 to 51 do
   for C2:=C1+1 to 51 do
      Cards[0] = P1;
      Cards[1] = C1;
      Cards[2] = C2;
      (* generate C3...C7 *)

(这仅仅是一个示范一卡P1,你就必须扩大,对于P2,但我认为这是简单的。虽然这将是一个很大的打字...)这样一来,排序不采取任何时候都。生成的排列已经订购了。

(this is just a demonstration for one card P1, you would have to expand that for P2, but I think that's straightforward. Although it'll be a lot of typing...)That way, the sorting doesn't take any time at all. The generated permutations are already ordered.

这篇关于什么是最快的方式,以7整数数组排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:12
查看更多