计数排序 Java 代码


计数排序 代码

/* Copyright (c) 2012 the authors listed at the following URL, and/or
the authors of referenced articles or incorporated external code:
https://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: https://en.literateprograms.org/Counting_sort_(Java)?oldid=11981
*/
import .util.Arrays;
import .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];

       if (a[i] > max)
           max = a[i];

           min = a[i];
   }
   int numValues = max - min + 1;
        int[] counts = new int[numValues];

       counts[a[i]-min]++;
   }
   int outputPos = 0;


           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();

            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

            if (a[i] != a2[i]) {
                System.out.println("Error: Results do not match.");
                return;
            }
        }
        System.out.println("Sort successful");
    }
}

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《计数排序 Java 代码
本文地址:https://www.zhiletu.com/archives-7827.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微