Java HeapSort
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;
}
}
Java HeapSort的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java演算法之堆排序(HeapSort)
import java.util.Arrays; publicclass HeapSort { inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,9 ...
- Heapsort 堆排序算法详解(Java实现)
Heapsort (堆排序)是最经典的排序算法之一,在google或者百度中搜一下可以搜到很多非常详细的解析.同样好的排序算法还有quicksort(快速排序)和merge sort(归并排序),选择 ...
- [转载]堆排序(HeapSort) Java实现
堆排序的思想是利用数据结构--堆.具体的实现细节: 1. 构建一个最大堆.对于给定的包含有n个元素的数组A[n],构建一个最大堆(最大堆的特性是,某个节点的值最多和其父节点的值一样大.这样,堆中的最大 ...
- heapsort(Java)(最小堆)
public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextI ...
- 堆排序Heapsort的Java和C代码
Heapsort排序思路 将整个数组看作一个二叉树heap, 下标0为堆顶层, 下标1, 2为次顶层, 然后每层就是"3,4,5,6", "7, 8, 9, 10, 11 ...
- 8个排序算法——java
public static void radixsort(int[] a){ int max=a[0]; for(int i=1;i<a.length;i++){ if (max<a[i] ...
- java排序学习笔记
前面写了js的排序实现,总得玩玩java的哈. 同样,冒泡.选择.快速(这三个之前实现过也写过文章).堆排序,然后做比较. 主要遇到的难点: - -||想轻松点写个封装计时的逻辑,不想每调用一个排序就 ...
- Java各种排序算法详解
排序大的分类可以分为两种:内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.下面讲的排序都是属于内排序. 内排序有可以分为以下几类: (1).插 ...
随机推荐
- php7的扩展库安装方法
转:https://www.cnblogs.com/to-be-rich/p/8001175.html 今天的知识点:1.php的再次编译不会对现有的php业务有影响,只有正式kill -USR2 p ...
- 移动端安卓和 IOS 开发框架 Framework7 布局
对应的各种效果,Framework7 里面实现的方式比较多,这里我就只写我用的一种,样式有的自己修改了的,想看官方详细的参见 http://framework7.cn 一.手风琴布局Accordion ...
- 【集成模型】Boosting
0 - 思想 Bagging算法思想是减少预测方差(variance),Boosting算法思想是为了减少预测偏差(bias). Boosting算法思想是将“弱学习算法”提升为“强学习算法”.一般来 ...
- python基础之知识补充-作用域、特殊语法
python作用域 无函数的作用域 在python中没有块级作用域 什么叫块级作用域呢?先来看个例子: if 1 == 1: name= 'alex' print(name) 运行结果为alex 在j ...
- ABAP Memory ID
转自:https://blog.csdn.net/lyq123333321/article/details/52659114 (一) Difference Between SAP a ...
- vs2015配置link.exe环境变量
https://www.cnblogs.com/johnwii/p/4966086.html
- LINQ语法详解
我会通过一些列的实例向大家讲解LINQ的语法. 先创建一个Person类,作为数据实体 public class Person { public string Name { get; set; } p ...
- 【Leetcode_easy】594. Longest Harmonious Subsequence
problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...
- 【ARTS】01_30_左耳听风-201900603~201900609
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- Infos - 通过搜索引擎获取信息与数据
常用搜索引擎命令 site 用来查询网站收录量. 比如site:http://www.cnblogs.com/ inurl 查URL中包含的元素,比如inurl:bbs ,搜索出URL包含bbs的页面 ...