Program work 13. Heap Sort in Java

来源:互联网 发布:java 哪里用到反射 编辑:程序博客网 时间:2024/05/16 11:07

关键在于最大堆的建立和重构 (其实两个过程是一样的)

因为堆是平衡二叉树, 所以数组可以直接用下标表示父子的节点关系, 数组要从下标1开始.

将最大值移到数组只需要O(n)

重构堆需要O(lgn)

所以upper bound is O(n*logn)

事实上, lower bound is  Ω(n*logn) 

最好最坏最差都是O(n*logn)


Pseudocode

heapSort (arr, len){    buildMaxHeap()    for () {        remove the root element(the max) to the end of array        reconstructHeap();    }}reconsturctHeap(arr, len){    tmp = arr[root]    get the children of root    while () {        get the bigger child between left and right child        if the element of that child is larger than root            replace its parent element with is        move to next children    }    place the tmp to the apporpriate pos}

Java

package ncku.cdc.sorting;import java.util.Random;public class HeapSort {  private int[] sequence;  public HeapSort(int size, int range) {    sequence = new int[size + 1];    Random rand = new Random();    for (int i = 1; i <= size; i++) {      sequence[i] = rand.nextInt(range);    }  }  public static void main(String[] args) {    int size = Integer.valueOf(args[0]);    int range = Integer.valueOf(args[1]);    HeapSort heap = new HeapSort(size, range);    System.out.println("before heapSort:");    SortingTools.validation(heap.getSequence(), 1);        heap.heapSort(heap.getSequence(), size);    System.out.println("after heapSort:");    SortingTools.validation(heap.getSequence(), 1);  }    public void heapSort(int[] arr, int len) {    for (int i = len / 2; i > 0; i--) {      retifyHeap(arr, i, len);    }    for (int j = len - 1; j > 0; j--) {      swap(arr, 1, j + 1);      retifyHeap(arr, 1, j);    }  }  private void retifyHeap(int[] arr, int root, int len) {    int tmp = arr[root];    int child = root * 2;    while (child <= len) {      if ((child < len) && (arr[child] < arr[child + 1])) { child++; }      if (tmp > arr[child]) { break; }       else {        arr[child / 2] = arr[child];        child *= 2;      }    }    arr[child / 2] = tmp;  }    private void swap(int[] arr, int i, int j) {    int t = arr[i];    arr[i] = arr[j];    arr[j] = t;  }  public int[] getSequence() {    return sequence;  }}


程序输入: 35 200. 表示随机生成一个长度为35, 数字范围在[0, 200)的数组

程序输出:

before heapSort:
67 18 192 142 136 30 144 6 158 11 123 34 36 155 97 12 7 159 81 135 160 102 48 42 0 155 77 114 139 146 186 32 42 144 100 
after heapSort:
0 6 7 11 12 18 30 32 34 36 42 42 48 67 77 81 97 100 102 114 123 135 136 139 142 144 144 146 155 155 158 159 160 186 192

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高尔夫旅行款被锁在车内怎么办 英雄联盟误删文件怎么办 拍拍贷换了号码怎么办 手机打开显示无法连接服务器怎么办 剑灵画面卡顿怎么办 cf被永久禁赛了怎么办 微信没有微游戏商店怎么办 游侠云盒下载慢怎么办2018 安卓手机玩网页游戏卡怎么办 safari点开什么都没有怎么办 康佳电视全网搜索打不开怎么办 脚被图钉扎了怎么办 电脑中毒了打不开软件怎么办 剑三程序不兼容怎么办 玩无主之地卡怎么办 平台老板跑路了怎么办 qq在苹果下载不了怎么办 下载速度快上传速度慢怎么办 苹果7开网页慢怎么办 为什么浏览器下载视频速度慢怎么办 机连WLAN网速慢怎么办 会声会影卸载后无法重新安装怎么办 电视空间不足无法卸载怎么办 堡垒之夜下载慢怎么办 手机网盘下载速度慢怎么办插件 笔记本电脑显示连接不可用怎么办 蓝魔手机充电慢怎么办 360f4手机充电慢怎么办 vivo卡了怎么办小窍门 白色衣服用84泡后变黄怎么办 用祛斑霜脱皮了怎么办 用祛斑霜脸一直蜕皮怎么办 吃热的就流鼻涕怎么办 键盘qaz失灵其他都没事怎么办 时时彩代理抓了怎么办 6p升级ios11卡顿怎么办 微信弄没了又换号了怎么办 快手账号异常请去激活怎么办 九游账号被转移怎么办 绝地求生刺激战场闪退怎么办 qq回执编号忘了怎么办