<?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Counting sort - Page 6 - Wikipedia">
<p>
<a accesskey="1" href="page.php?w=counting_sort&amp;p=5">1.Previous</a><br />
<a accesskey="3" href="page.php?w=counting_sort&amp;p=7">3.Next</a>
</p>
<p>of the elements ordered by their keys. Because of its application to radix sorting, counting sort must be a <a href="page.php?w=stable_sort">stable sort</a>; that is, if two elements share the same key, their relative order in the output array and their relative order in the input array should match.</p>

<p><big>Pseudocode</big></p>
<p>In pseudocode, the algorithm may be expressed as:</p>

<p> <b>function</b> CountingSort(input, k) <b>is</b>          count <- array of k</i> + 1 zeros     output <- array of same length as input          <b>for i = 0 <b>to</b> length(input) - 1 <b>do</b>         j = key(input[i])         count[j] = count[j] + 1      <b>for</b> i = 1 <b>to</b> k <b>do</b>         count[i] = count[i] + count[i - 1]      <b>for</b> i = length(input) - 1 <b>down to</b> 0 <b>do</b>         j = key(input[i])         count[j] = count[j] - 1         output[count[j]] = input[i]      <b>return</b> output</-></-></p><p>
<a accesskey="1" href="page.php?w=counting_sort&amp;p=5">1.Previous</a><br />
<a accesskey="3" href="page.php?w=counting_sort&amp;p=7">3.Next</a>
</p>

<do type="prev" label="Search">
        <go href="search.wml"/>
</do>

</card>
</wml>
