计数排序

来源:互联网 发布:搜狗排名优化 编辑:程序博客网 时间:2024/06/05 15:57


Suppose we have an input array $ \mathtt{a}$ consisting of $ \mathtt{n}$ integers, each in the range $ 0,\ldots,\ensuremath{\mathtt{k}}-1$. The counting-sortalgorithm sorts $ \mathtt{a}$ using an auxiliary array $ \mathtt{c}$ of counters. It outputs a sorted version of $ \mathtt{a}$ as an auxiliary array $ \mathtt{b}$.

The idea behind counting-sort is simple: For each $ \ensuremath{\mathtt{i}}\in\{0,\ldots,\ensuremath{\mathtt{k}}-1\}$, count the number of occurrences of $ \mathtt{i}$ in $ \mathtt{a}$and store this in $ \mathtt{c[i]}$. Now, after sorting, the output will look like $ \mathtt{c[0]}$ occurrences of 0, followed by $ \mathtt{c[1]}$occurrences of 1, followed by $ \mathtt{c[2]}$ occurrences of 2,..., followed by $ \mathtt{c[k-1]}$ occurrences of $ \mathtt{k-1}$.


The first $ \mathtt{for}$ loop in this code sets each counter $ \mathtt{c[i]}$ so that it counts the number of occurrences of $ \mathtt{i}$ in $ \mathtt{a}$. By using the values of $ \mathtt{a}$ as indices, these counters can all be computed in $ O(\ensuremath{\mathtt{n}})$ time with a single for loop. At this point, we could use $ \mathtt{c}$ to fill in the output array $ \mathtt{b}$ directly. However, this would not work if the elements of $ \mathtt{a}$have associated data. Therefore we spend a little extra effort to copy the elements of $ \mathtt{a}$ into $ \mathtt{b}$.

The next $ \mathtt{for}$ loop, which takes $ O(\ensuremath{\mathtt{k}})$ time, computes a running-sum of the counters so that $ \mathtt{c[i]}$ becomes the number of elements in $ \mathtt{a}$ that are less than or equal to $ \mathtt{i}$. In particular, for every $ \ensuremath{\mathtt{i}}\in\{0,\ldots,\ensuremath{\mathtt{k}}-1\}$, the output array, $ \mathtt{b}$, will have

$\displaystyle \ensuremath{\mathtt{b[c[i-1]]}}=\ensuremath{\mathtt{b[c[i-1]+1]=}}\cdots=\ensuremath{\mathtt{b[c[i]-1]}}=\ensuremath{\mathtt{i}} \enspace .$

Finally, the algorithm scans $ \mathtt{a}$ backwards to place its elements, in order, into an output array $ \mathtt{b}$. When scanning, the element $ \mathtt{a[i]=j}$ is placed at location $ \mathtt{b[c[j]-1]}$ and the value $ \mathtt{c[j]}$ is decremented.

Theorem 11..7   The $ \mathtt{countingSort(a,k)}$ method can sort an array $ \mathtt{a}$ containing $ \mathtt{n}$ integers in the set $ \{0,\ldots,\ensuremath{\mathtt{k}}-1\}$ in $ O(\ensuremath{\mathtt{n}}+\ensuremath{\mathtt{k}})$ time.

The counting-sort algorithm has the nice property of being stable; it preserves the relative order of equal elements. If two elements $ \mathtt{a[i]}$ and $ \mathtt{a[j]}$ have the same value, and $ \ensuremath{\mathtt{i}}<\ensuremath{\mathtt{j}}$ then $ \mathtt{a[i]}$ will appear before $ \mathtt{a[j]}$ in $ \mathtt{b}$. This will be useful in the next section.



public class CountSort{    static  int[] countSort(int[] a,int k){        //k max element in array a        // the value of the element in a  server as the index for c , the key point        int[] c=new int[k];        for (int i = 0; i < a.length; i++)        {            c[a[i]]++;//same element in a will count the same element in c array        }        // from 1,not 0        for (int i = 1; i <k; i++)        {            c[i]+=c[i-1];        }        int[] b=new int[a.length];        for (int i=a.length-1; i >-1; i--)        {            //-- operation is to process the duplicate element, the value in array is changed. pay attention             b[--c[a[i]]]=a[i];        }        return b;    }        public static int getMax(int[] arr) {        int max = arr[0];        for (int i = 0; i < arr.length; i++) {            if (arr[i] > max)                max = arr[i];        }        return max;    }        public static void display(int[] R)    {        System.out.println();        for (int i = 0; i < R.length; i++)        {            System.out.print(R[i] + " ");        }    }    public static void main(String[] args)    {        int[] r = { 2,3,1,5,1,6,9,9 };        display(r);        int k=getMax(r)+1;        int[] b=countSort(r,k);        display(b);    }}


原创粉丝点击