排序算法三:堆排序(Heapsort)
堆排序(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)的更多相关文章
- 排序算法FOUR:堆排序HeapSort
/** *堆排序思路:O(nlogn) * 用最大堆,传入一个数组,先用数组建堆,维护堆的性质 * 再把第一个数与堆最后一个数调换,因为第一个数是最大的 * 把堆的大小减小一 * 再 在堆的大小上维护 ...
- 排序算法三:Shell插入排序
排序算法三:Shell插入排序 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 引言 在我的博文<"主宰世界"的10种算法短评> ...
- Java常见排序算法之堆排序
在学习算法的过程中,我们难免会接触很多和排序相关的算法.总而言之,对于任何编程人员来说,基本的排序算法是必须要掌握的. 从今天开始,我们将要进行基本的排序算法的讲解.Are you ready?Let ...
- 排序算法之堆排序(Heapsort)解析
一.堆排序的优缺点(pros and cons) (还是简单的说说这个,毕竟没有必要浪费时间去理解一个糟糕的的算法) 优点: 堆排序的效率与快排.归并相同,都达到了基于比较的排序算法效率的峰值(时间复 ...
- 《排序算法》——堆排序(大顶堆,小顶堆,Java)
十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...
- Java排序算法之堆排序
堆的概念: 堆是一种完全二叉树,非叶子结点 i 要满足key[i]>key[i+1]&&key[i]>key[i+2](最大堆) 或者 key[i]<key[i+1] ...
- 数据结构与算法之PHP排序算法(堆排序)
一.堆的定义 堆通常是一个可以被看做一棵树的数组对象,其任一非叶节点满足以下性质: 1)堆中某个节点的值总是不大于或不小于其父节点的值: 每个节点的值都大于或等于其左右子节点的值,称为大顶堆.即:ar ...
- C++编程练习(13)----“排序算法 之 堆排序“
堆排序 堆是具有下列性质的完全二叉树:每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆(也叫最大堆):或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆(也叫最小堆). 最小堆和最大堆如 ...
- 八大排序算法之七—堆排序(Heap Sort)
堆排序是一种树形选择排序,是对直接选择排序的有效改进. 基本思想: 堆的定义如下:具有n个元素的序列(k1,k2,...,kn),当且仅当满足 时称之为堆.由堆的定义可以看出,堆顶元素(即第一个元素) ...
随机推荐
- Vue.js——60分钟快速入门 一
来源:https://www.cnblogs.com/keepfool/p/5619070.html Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组 ...
- linux学习笔记(1):
一.Linux系统简介 1.什么是linux Linux是一个免费的.多用户.多任务的操作系统,其运行方式.功能和UNIX系统很相似,但Linux系统的稳定性.安全性与网络功能是许多商业操作系统所无法 ...
- Linux安装postgresql及基础操作
安装环境说明 系统环境说明 [root@slave1 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@sl ...
- 初步理解React
1.组件化 在 MV* 架构出现之前,组件主要分为两种: 狭义上的组件,又称为 UI 组件,比如 Tabs 组件.Dropdown 组件.组件主要围绕在交互 动作上的抽象,针对这些交互动作,利用 Ja ...
- 13DBUtils工具类
如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils. DBUtils就是JDBC的简化开发工具包.需要项 ...
- python如何查看内存占用空间
我们如何查看变量占用了多少内存空间呢 首先我们引用sys模块,在使用getsizeof()方法 import sys L = [x for x in range(10000)] print(sys.g ...
- php内置函数分析之strtoupper()、strtolower()
strtoupper(): PHP_FUNCTION(strtoupper) { zend_string *str; ZEND_PARSE_PARAMETERS_START(, ) Z_PARAM_S ...
- 测试tensorflowgpu版本是否可用
输入一下代码即可 import tensorflow as tf print(tf.test.is_gpu_available())
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- Vuex----核心概念和API
state 1)vuex管理状态的对象 2)它应该是唯一的 const state = { xxx:initValue } mutations 1)包含多个直接更新state的方法(回调函数)的对象 ...