Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the gist (you accidentally linked both to the same), you have a few typos</p> <pre><code>public static List&lt;Integer&gt; execSort(List&lt;Integer&gt; s) { int start = (s.size()/2)-1; int end = s.size()-1; while( start &gt;= 0){ s = sift(s, start, end); </code></pre> <p><code>sift</code> takes the count as last argument, not the last index, so the argument ought to be <code>s.size()</code> (or <code>end+1</code>) instead of <code>end</code>.</p> <pre><code>public static List&lt;Integer&gt; sift(List&lt;Integer&gt; s, int start, int count){ int root = start; while( ((root*2)+1) &lt; 2 ){ </code></pre> <p>That must be <code>while(root*2+1 &lt; count)</code>, and not <code>&lt; 2</code>.</p> <p>In the code you have here, you have in part the same problem (caused by an odd indexing strategy, I suspect):</p> <pre><code> for(int i = n/2; i&gt;0; i--){ s = downheap(s, i, n); </code></pre> <p>Since you always <code>get(i-1)</code> resp. <code>j-1</code> in <code>downheap</code>, you need an upper bound of <code>s.size()</code> or <code>n+1</code> while building the initial heap.</p> <pre><code> } while(n &gt;= 1){ </code></pre> <p>This loop should only run while <code>n &gt; 1</code>, or you'll swap the smallest element out of place.</p> <pre><code> t= s.remove(0); s.add(0, s.remove(n-1)); s.add(n-1, t); </code></pre> <p>The old root must go in the last place, that's place <code>n</code>, not <code>n-1</code>, <code>s.add(n,t)</code>.</p> <pre><code> n--; s = downheap(s, 1, n); } </code></pre> <p>In <code>downheap</code>, the final</p> <pre><code> s.set(i-1, t); </code></pre> <p>is superfluous, you always swap <code>t</code>, so when that line is reached, the element at <code>i-1</code> already is <code>t</code>.</p>
    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