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

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

297 ℃
     
public void heapSort2(int[] nums) {
  for(int i = nums.length/2-1; i >= 0; i--){
      sift(nums, i, nums.length);
  }
  for (int i = nums.length-1; i > 0; i--) {
      int temp = nums[0];
      nums[0] = nums[i];
      nums[i] = temp;
      sift(nums, 0, i);
  }
}

private void sift(int[] nums, int parent, int len) {
  int value = nums[parent];
  for (int child = 2*parent +1; child < len; child = child*2 +1) {
      if(child+1 < len && nums[child+1] > nums[child]){
          child++;
      }
      if(nums[child] > value){
          nums[parent] = nums[child];
          parent = child;
      } else {
          break;
      }
  }
  nums[parent] = value;
}

PS:大顶堆实现升序,每次将最大值移到堆的最后一个位置上

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

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

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

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

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

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

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

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

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

当前位置: 网站首页 > 后端开发
本文共计477个字,预计阅读时长4分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一个免费可商用psd文件素材网站——FreePik
下一篇: 一行代码轻松实现优雅的过渡动画插件——AutoAnimate
x 打工人ai神器