堆排序(Heapsort)是一种利用数据结构中的堆进行排序的算法,分为构建初始堆,减小堆的元素个数,调整堆共3步。

(一)算法实现

     protected void sort(int[] toSort) {
buildHeap(toSort);
for (int i = toSort.length - 1; i > 0; i--) {
CommonUtils.swap(toSort, 0, i);
adjustHeap(toSort, 0, i);
}
} /**
*
* @param toSort
* array of heap, begins from 0
* @param index
* index to adjust
* @param size
* size of heap
*/
private void adjustHeap(int[] toSort, int index, int size) {
int leftNode = index * 2 + 1;
int rightNode = leftNode + 1; int maxIndex = index;
if (leftNode < size && toSort[leftNode] > toSort[maxIndex]) {
maxIndex = leftNode;
}
if (rightNode < size && toSort[rightNode] > toSort[maxIndex]) {
maxIndex = rightNode;
}
if (maxIndex != index) {
CommonUtils.swap(toSort, index, maxIndex);
adjustHeap(toSort, maxIndex, size);
} } /**
*
* @param toSort
* array to sort
*/
private void buildHeap(int[] toSort) {
int lastNonLeaf = toSort.length / 2 - 1;
for (int i = lastNonLeaf; i >= 0; i--) {
adjustHeap(toSort, i, toSort.length);
}
}

Heap sort

1)堆排序是原地排序

2)堆排序的时间复杂度是O(nlgn)

3)堆排序属于比较排序

4)堆排序不是稳定排序算法

(二)仿真结果

**************************************************
Number to Sort is:2500
Array to sort is:{419836,72576,347420,355422,378503,65556,443634,137868,266344,918856...}
Cost time of 【HeapSort】 is(milliseconds):1
Sort result of 【HeapSort】:{185,874,996,1232,1448,2357,2728,2854,3137,3291...}
**************************************************
Number to Sort is:25000
Array to sort is:{169570,655593,54301,59080,890711,224726,720131,590749,600165,681962...}
Cost time of 【HeapSort】 is(milliseconds):7
Sort result of 【HeapSort】:{9,107,119,192,297,321,338,359,359,362...}
**************************************************
Number to Sort is:250000
Array to sort is:{233097,327821,972339,26697,803510,598167,178244,117664,904299,195258...}
Cost time of 【HeapSort】 is(milliseconds):59
Sort result of 【HeapSort】:{0,1,3,8,16,24,32,37,45,52...}

相关代码:

 package com.cnblogs.riyueshiwang.sort;

 import java.util.Arrays;

 public class HeapSort extends abstractSort {
@Override
protected void sort(int[] toSort) {
buildHeap(toSort);
for (int i = toSort.length - 1; i > 0; i--) {
CommonUtils.swap(toSort, 0, i);
adjustHeap(toSort, 0, i);
}
} /**
*
* @param toSort
* array of heap, begins from 0
* @param index
* index to adjust
* @param size
* size of heap
*/
private void adjustHeap(int[] toSort, int index, int size) {
int leftNode = index * 2 + 1;
int rightNode = leftNode + 1; int maxIndex = index;
if (leftNode < size && toSort[leftNode] > toSort[maxIndex]) {
maxIndex = leftNode;
}
if (rightNode < size && toSort[rightNode] > toSort[maxIndex]) {
maxIndex = rightNode;
}
if (maxIndex != index) {
CommonUtils.swap(toSort, index, maxIndex);
adjustHeap(toSort, maxIndex, size);
} } /**
*
* @param toSort
* array to sort
*/
private void buildHeap(int[] toSort) {
int lastNonLeaf = toSort.length / 2 - 1;
for (int i = lastNonLeaf; i >= 0; i--) {
adjustHeap(toSort, i, toSort.length);
}
} public static void main(String[] args) {
for (int j = 0, n = 2500; j < 3; j++, n = n * 10) {
System.out
.println("**************************************************");
System.out.println("Number to Sort is:" + n);
int[] array = CommonUtils.getRandomIntArray(n, 1000000);
System.out.print("Array to sort is:");
CommonUtils.printIntArray(array); int[] array1 = Arrays.copyOf(array, n);
new HeapSort().sortAndprint(array1);
}
}
}

HeapSort.java

排序算法三:堆排序(Heapsort)的更多相关文章

  1. 排序算法FOUR:堆排序HeapSort

    /** *堆排序思路:O(nlogn) * 用最大堆,传入一个数组,先用数组建堆,维护堆的性质 * 再把第一个数与堆最后一个数调换,因为第一个数是最大的 * 把堆的大小减小一 * 再 在堆的大小上维护 ...

  2. 排序算法三:Shell插入排序

    排序算法三:Shell插入排序 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 引言 在我的博文<"主宰世界"的10种算法短评> ...

  3. Java常见排序算法之堆排序

    在学习算法的过程中,我们难免会接触很多和排序相关的算法.总而言之,对于任何编程人员来说,基本的排序算法是必须要掌握的. 从今天开始,我们将要进行基本的排序算法的讲解.Are you ready?Let ...

  4. 排序算法之堆排序(Heapsort)解析

    一.堆排序的优缺点(pros and cons) (还是简单的说说这个,毕竟没有必要浪费时间去理解一个糟糕的的算法) 优点: 堆排序的效率与快排.归并相同,都达到了基于比较的排序算法效率的峰值(时间复 ...

  5. 《排序算法》——堆排序(大顶堆,小顶堆,Java)

    十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...

  6. Java排序算法之堆排序

    堆的概念: 堆是一种完全二叉树,非叶子结点 i 要满足key[i]>key[i+1]&&key[i]>key[i+2](最大堆) 或者 key[i]<key[i+1] ...

  7. 数据结构与算法之PHP排序算法(堆排序)

    一.堆的定义 堆通常是一个可以被看做一棵树的数组对象,其任一非叶节点满足以下性质: 1)堆中某个节点的值总是不大于或不小于其父节点的值: 每个节点的值都大于或等于其左右子节点的值,称为大顶堆.即:ar ...

  8. C++编程练习(13)----“排序算法 之 堆排序“

    堆排序 堆是具有下列性质的完全二叉树:每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆(也叫最大堆):或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆(也叫最小堆). 最小堆和最大堆如 ...

  9. 八大排序算法之七—堆排序(Heap Sort)

    堆排序是一种树形选择排序,是对直接选择排序的有效改进. 基本思想: 堆的定义如下:具有n个元素的序列(k1,k2,...,kn),当且仅当满足 时称之为堆.由堆的定义可以看出,堆顶元素(即第一个元素) ...

随机推荐

  1. java 标识接口的作用

    标识接口的作用 标识接口是没有任何方法和属性的接口.标识接口不对实现它的类有任何语义上的要求,它仅仅表明实现它的类属于一个特定的类型. 标接口在Java语言中有一些很著名的应用,例如我们常用的Arra ...

  2. 解决GitHub push项目——Push failed: Unable to access 'https://********.git/': Failed to connect to 127.0.0.1 port 1080: Connection refused

    解决方法: 第一步:在git中设置http代理 git config --global http.proxy 第二步:在git中取消http代理 git config --global --unset ...

  3. srs-librtmp pusher(push h264 raw)

    Simple Live System Using SRS https://www.cnblogs.com/dong1/p/5100792.html 1.上面是推送文件,改成推送缓存 封装了三个函数 i ...

  4. RocketMQ集群部署安装

    RcoketMQ:[ 1.低延时:在高压下,1毫秒内超过99.6%的反应延迟. 2.面向金融:具有跟踪和审计功能的高可用性. 3.行业可持续发展:保证了万亿级的消息容量. 4.厂商中立:一个新的开放的 ...

  5. python面向对象--类的内置函数

    #isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...

  6. 1146. Topological Order (25)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  7. 函数&&变量

    #*- encoding=utf-8 -*import sysprint(sys.getdefaultencoding()) def test(x,y,z): print(x) print(y) pr ...

  8. JS原型和原型链(3)

    构造函数创建对象: function Person() { } var person = new Person(); person.name = 'Kevin'; console.log(person ...

  9. Java+Maven的工程运行Sonar的方式

    step 1:在maven->setting.xml中进行配置 修改mvn工程所用的setting.xml文件,在<profiles></profiles>节点中增加: ...

  10. Test 6.24 T2 集合

    问题描述 有一个可重集合,一开始只有一个元素 0. 你可以进行若干轮操作,每轮你需要对集合中每个元素 x 执行以下三种操作之一: 将 x 变为 x+1; 选择两个非负整数 y,z 满足 y+z=x , ...