Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the fastest possible way to sort an array of 7 integers?
    primarykey
    data
    text
    <p>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.</p> <p>I use this type (among others, of course):</p> <pre><code> type T7Cards = array[0..6] of integer; </code></pre> <p>There are two things about this array that may be important when deciding how to sort it:</p> <ol> <li>Every item is a value from 0 to 51. No other values are possible. </li> <li>There are no duplicates. Never.</li> </ol> <p>With this information, what is the <em>absolutely fastest</em> way to sort this array? I use Delphi, so pascal code would be the best, but I can read C and pseudo, albeit a bit more slowly :-)</p> <p>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.</p> <p>EDIT:</p> <p>Mason Wheeler asked why it's necessary to optimize. One reason is that the method will be called 2118760 times.</p> <p>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)</p> <p>If I have two cards in the pocket, P1 and P2, I will use the following loops to generate all possible combinations:</p> <pre><code>for C1 := 0 to 51-4 do if (C1&lt;&gt;P1) and (C1&lt;&gt;P2) then for C2 := C1+1 to 51-3 do if (C2&lt;&gt;P1) and (C2&lt;&gt;P2) then for C3 := C2+1 to 51-2 do if (C3&lt;&gt;P1) and (C3&lt;&gt;P2) then for C4 := C3+1 to 51-1 do if (C4&lt;&gt;P1) and (C4&lt;&gt;P2) then for C5 := C4+1 to 51 do if (C5&lt;&gt;P1) and (C5&lt;&gt;P2) then begin //This code will be executed 2 118 760 times inc(ComboCounter[GetComboFromCards([P1,P2,C1,C2,C3,C4,C5])]); end; </code></pre> <p>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.</p> <p>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 :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload