文章图片标题

计数排序 Java 代码

分类:算法与数据结构 作者:阳光倾城 评论:0 点击: 376 次 日期:2016-08-01


计数排序 Java 代码

/* Copyright (c) 2012 the authors listed at the following URL, and/or
the authors of referenced articles or incorporated external code:
http://en.literateprograms.org/Counting_sort_(Java)?action=history&offset=20080109075430
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Retrieved from: http://en.literateprograms.org/Counting_sort_(Java)?oldid=11981
*/
import java.util.Arrays;
import java.util.Random;
public class CountingSort {
    /** Sorts array a where each element is between 0 and numValues-1. */
    public static void sort(int[] a) {
        if (a.length == 0)
     return;
   int max = a[0], min = a[0];
   for (int i = 1; i < a.length; i++) {
       if (a[i] > max)
           max = a[i];
       else if (a[i] < min)
           min = a[i];
   }
   int numValues = max - min + 1;
        int[] counts = new int[numValues];
        for (int i = 0; i < a.length; i++) {
       counts[a[i]-min]++;
   }
   int outputPos = 0;
   for (int i = 0; i < numValues; i++) {
       for (int j = 0; j < counts[i]; j++) {
           a[outputPos] = i+min;
           outputPos++;
       }
   }
    }
    public static void main(String[] args) {
        int listSize = Integer.parseInt(args[0]);
        int numValues = Integer.parseInt(args[1]);
        // Generate list of random values
        int[] a = new int[listSize];
        Random r = new Random();
        for (int i = 0; i < a.length; i++) {
            a[i] = r.nextInt(numValues);
        }
        // Copy list to new array
        int[] a2 = new int[listSize];
        System.arraycopy(a, 0, a2, 0, listSize);
        // Sort the two arrays
        sort(a);
        Arrays.sort(a2);
        // Compare the two arrays
        for (int i = 0; i < listSize; i++) {
            if (a[i] != a2[i]) {
                System.out.println("Error: Results do not match.");
                return;
            }
        }
        System.out.println("Sort successful");
    }
}




声明: 除非注明,本文属( 阳光倾城 )原创,转载请保留链接: http://www.tomrrow.com/archives-7827.html