Note that there are some explanatory texts on larger screens.

plurals
  1. POSelection Rank Algorithm
    text
    copied!<p>Below is an implementation of the selection rank algorithm, which is used to find the elements before the Kth smallest elements in an array. Sometimes this program works well, but it sometimes fails due to stack overflow error (error below code snippet)</p> <pre class="lang-java prettyprint-override"><code>public static int rand(int left, int right){ return left+(int)(Math.random()*(double)(right-left)); } public static int rank(int[] array, int left, int right, int rank){ int pivot = rand(left, right); int leftend = partition(array, left, right, array[pivot]); int size = leftend - left +1; if(size == rank+1){ for(int i=0; i&lt;=leftend; i++) System.out.print(array[i]+" ,"); return 0; }else if(size &gt; rank) return rank(array, left, leftend, rank); else return rank(array, leftend+1, right, rank - size); } public static int partition(int[] array, int left, int right, int pivot){ while(true){ while(left &lt;= right &amp;&amp; array[left] &lt;= pivot) left++; while(right &gt;= left &amp;&amp; array[right] &gt; pivot) right--; if(left &gt; right) return left-1; int temp = array[left]; array[left] = array[right]; array[right] = temp; } } </code></pre> <p>Error:</p> <pre><code>Exception in thread "main" java.lang.StackOverflowError at java.util.Random.nextDouble(Random.java:409) at java.lang.Math.random(Math.java:712) at mod.rand(mod.java:12) at mod.rank(mod.java:16) at mod.rank(mod.java:25) </code></pre> <p>I think maybe rand() is causing this problem, but I am not sure. Nor do I have any idea how to fix it.</p>
 

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