web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

java常见排序算法——桶排序(附代码示列)

249 ℃
     
public void bucketSort(int[] nums){
  int max = Integer.MIN_VALUE;
  int min = Integer.MAX_VALUE;
  for(int num : nums){
    max = Math.max(max, num);
    min = Math.min(min, num);
  }
  int bucketCount = (max-min)/nums.length+1;
  List<List<Integer>> bucketList = new ArrayList<>();
  for (int i = 0; i < bucketCount; i++) {
    bucketList.add(new ArrayList<>());
  }

  for(int num : nums){
    int index = (num-min)/nums.length;
    bucketList.get(index).add(num);
  }
  for(List<Integer> bucket : bucketList){
    Collections.sort(bucket);
  }

  int j = 0;
  for(List<Integer> bucket : bucketList){
    for(int num : bucket){
      nums[j] = num;
      j++;
    }
  }

}

PS:类似计数排序,不同点在于统计的是某个区间(桶)里的数。

java常见排序算法——基数排序(附代码示列)

java常见排序算法——计数排序(附代码示列)

java常见排序算法——堆排序(附代码示列)

java常见排序算法——希尔排序(附代码示列)

java常见排序算法——归并排序(附代码示列)

标签: java排序算法, 桶排序

上面是“java常见排序算法——桶排序(附代码示列)”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_4262.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > 后端开发
本文共计547个字,预计阅读时长4分钟
生活小工具,收录了80多款小工具
上一篇: 基于JavaScrip的web动画库——GSAP
下一篇: 免费商用中文字体——优设鲨鱼菲特健康体
x 打工人ai神器