Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For any optimization, it's always best to test, test, test. I would try at least sorting networks and insertion sort. If I were betting, I'd put my money on insertion sort based on past experience.</p> <p>Do you know anything about the input data? Some algorithms will perform better with certain kinds of data. For example, insertion sort performs better on sorted or almost-sorted dat, so it will be the better choice if there's an above-average chance of almost-sorted data.</p> <p>The algorithm you posted is similar to an insertion sort, but it looks like you've minimized the number of swaps at the cost of more comparisons. Comparisons are far more expensive than swaps, though, because branches can cause the instruction pipeline to stall.</p> <p>Here's an insertion sort implementation:</p> <pre><code>static __inline__ int sort6(int *d){ int i, j; for (i = 1; i &lt; 6; i++) { int tmp = d[i]; for (j = i; j &gt;= 1 &amp;&amp; tmp &lt; d[j-1]; j--) d[j] = d[j-1]; d[j] = tmp; } } </code></pre> <p>Here's how I'd build a sorting network. First, use <a href="http://pages.ripco.net/~jgamble/nw.html" rel="noreferrer">this site</a> to generate a minimal set of SWAP macros for a network of the appropriate length. Wrapping that up in a function gives me:</p> <pre><code>static __inline__ int sort6(int * d){ #define SWAP(x,y) if (d[y] &lt; d[x]) { int tmp = d[x]; d[x] = d[y]; d[y] = tmp; } SWAP(1, 2); SWAP(0, 2); SWAP(0, 1); SWAP(4, 5); SWAP(3, 5); SWAP(3, 4); SWAP(0, 3); SWAP(1, 4); SWAP(2, 5); SWAP(2, 4); SWAP(1, 3); SWAP(2, 3); #undef SWAP } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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