Java HeapSort

/**
* <html>
* <body>
* <P> Copyright 1994-2018 JasonInternational </p>
* <p> All rights reserved.</p>
* <p> Created on 2018年4月10日 </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.algorithm.sorts; /**
* Heapsort is a comparison-based sorting algorithm to create a sorted array (or
* list), and is part of the selection sort family. Although somewhat slower in
* practice on most machines than a well-implemented quicksort, it has the
* advantage of a more favorable worst-case O(n log n) runtime.
* <p>
* Family: Selection.<br>
* Space: In-place.<br>
* Stable: False.<br>
* <p>
* Average case = O(n*log n)<br>
* Worst case = O(n*log n)<br>
* Best case = O(n*log n)<br>
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Heap_sort">Heap Sort (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class HeapSort<T extends Comparable<T>> { private HeapSort() { } public static <T extends Comparable<T>> T[] sort(T[] unsorted) {
createHeap(unsorted);
sortHeap(unsorted);
return unsorted;
} private static <T extends Comparable<T>> void sortHeap(T[] unsorted) {
int length = unsorted.length;
for (int index = length - 1; index > 0; index--) {
swap(0, index, unsorted); // swap root with the last heap element
int i = 0; // index of the element being moved down the tree
while (true) {
int left = (i * 2) + 1;
if (left >= index) // node has no left child
break;
int right = left + 1;
if (right >= index) { // node has a left child, but no right child
if (unsorted[left].compareTo(unsorted[i]) > 0)
swap(left, i, unsorted); // if left child is greater than node
break;
}
T ithElement = unsorted[i];
T leftElement = unsorted[left];
T rightElement = unsorted[right];
if (ithElement.compareTo(leftElement) < 0) { // (left > i)
if (unsorted[left].compareTo(rightElement) > 0) { // (left > right)
swap(left, i, unsorted);
i = left;
continue;
}
// (left > i)
swap(right, i, unsorted);
i = right;
continue;
}
// (i > left)
if (rightElement.compareTo(ithElement) > 0) {
swap(right, i, unsorted);
i = right;
continue;
}
// (n > left) & (n > right)
break;
}
}
} private static <T extends Comparable<T>> void createHeap(T[] unsorted) {
// Creates a max heap
int size = 0;
int length = unsorted.length;
for (int i = 0; i < length; i++) {
T e = unsorted[i];
size = add(size, e, unsorted);
}
} private static <T extends Comparable<T>> int add(int size, T element, T[] unsorted) {
int length = size;
int i = length;
unsorted[length++] = element;
T e = unsorted[i];
int parentIndex = ((i - 1) / 2);
T parent = unsorted[parentIndex];
while (e.compareTo(parent) > 0) {
swap(parentIndex, i, unsorted);
i = parentIndex;
e = unsorted[i];
parentIndex = ((i - 1) / 2);
parent = unsorted[parentIndex];
}
return length;
} private static <T extends Comparable<T>> void swap(int parentIndex, int childIndex, T[] unsorted) {
T parent = unsorted[parentIndex];
unsorted[parentIndex] = unsorted[childIndex];
unsorted[childIndex] = parent;
}
}

  

最新文章

  1. Windows Azure Platform Introduction (11) 了解Org ID、Windows Azure订阅、账户
  2. CF440C
  3. js标签放在html的什么位置比较好
  4. 序列化之Parcelable
  5. HW4.31
  6. 【百度地图API】如何制作“从这里出发”“到这里去”——公交篇
  7. ADFS 2.0 配置简介 PartⅠ – 安装ADFS
  8. MongoDB聚合
  9. java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
  10. 《SQL CookBook 》笔记-第一章-检索记录
  11. Wannafly挑战赛1 C MMSet2 虚树
  12. java实现带空格字符串的倒序输出
  13. LCA - Tarjan 算法
  14. 使用gitbook plugin
  15. Android -- 状态栏高度
  16. servlet中请求转发(forword)和重定向(redirect)的区别
  17. [SHELL]输出目录下所有的可执行文件,批量创建用户
  18. last-child 选择器
  19. 【动态规划】矩形嵌套 (DGA上的动态规划)
  20. Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

热门文章

  1. OpenJudge计算概论-异常细胞检测
  2. mp4文件格式解析(转)
  3. SurfaceView概述和基本使用
  4. 贝济埃曲线quadTo与传统的手势轨迹平滑度对比分析
  5. 自定义ObjectAnimator属性实现
  6. IO流的标准处理代码
  7. 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)
  8. osg qt 三维模型加载
  9. java判断字符串是否中文、日文
  10. QML渐变色