 <p>For sorting numbers you can use counting sort algorithm: you have numbers of limited range, from 0 to 999. So, you can count how many times each number occurred in input file:</p> <pre><code>int[] count = new int[1000]; //depending of scope, maybe you need to fill array with zeroes ... int number = ...;//read number from file; count[number]++; </code></pre> <p>Now, in order to output it as sorted, you just need two loops like this:</p> <pre><code>for (int i = 0; i &lt; 1000; ++i) { for (int j = 0; j &lt; count[i]; ++j) { System.out.println(i); } } </code></pre> <p>I wrote this just to make sure idea is clear, although your task doesn't require to output them all. Now, let's imagine you read these two numbers;</p> <pre><code>int start, finish = 0; //value read from input </code></pre> <p>We gonna modify the loops above like this:</p> <pre><code>int counter = 0; for (int i = 0; i &lt; 1000; ++i) { for (int j = 0; j &lt; count[i]; ++j) { if ((counter &gt;= start) &amp;&amp; (counter &lt; finish)) {//or maybe &lt;=, depending on what exactly do you need System.out.println(i); } counter++; if (counter &gt; finish) { //we finished, further iterations are waste of CPU time break(2); } } } </code></pre> <p>This method is not optimal, but easy to understand and implement. For more fast solutions, you may want to get rid of inner loop, replacing it with logic to add <code>count[i]</code> to <code>counter</code> at once and detecting moment when we need to start output.</p>