堆排序——HeapSort
基本思想:

图示: (88,85,83,73,72,60,57,48,42,6)

平均时间复杂度:
O(NlogN)由于每次重新恢复堆的时间复杂度为O(logN),共N - 1次重新恢复堆操作,再加上前面建立堆时N / 2次向下调整,每次调整时间复杂度也为O(logN)。二次操作时间相加还是O(N * logN)。
Java代码实现:
- public class HeapSortTest {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = new int[] { 10, 3, 2, 5, 6, 1, -2, 3, 14, 12, 3, 8, 55, 44,
- -10 };
- print(arr);
- heapSort(arr);
- System.out.println("排序后的数组:");
- print(arr);
- }
- private static void print(int[] a) {
- for (int i = 0; i < a.length; i++) {
- System.out.print(a[i] + "\t");
- }
- System.out.println();
- }
- private static void swap(int[] a, int i, int j) {
- a[i] = a[i] + a[j];
- a[j] = a[i] - a[j];
- a[i] = a[i] - a[j];
- }
- private static void heapSort(int[] a) {
- for (int i = a.length - 1; i >= 0; i--) {
- createMaxHeap(a, i);
- swap(a, 0, i);
- print(a);
- }
- }
- private static void createMaxHeap(int[] a, int lastIndex) {
- for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
- int k = i;
- while ((2 * k + 1) <= lastIndex) {
- int biggerIndex = 2 * k + 1;
- if (biggerIndex < lastIndex) {
- if (a[biggerIndex] < a[biggerIndex + 1]) {
- biggerIndex++;
- }
- }
- if (a[k] < a[biggerIndex]) {
- swap(a, k, biggerIndex);
- k = biggerIndex;
- } else {
- break;
- }
- }
- }
- }
- }
堆排序——HeapSort的更多相关文章
- 堆排序 Heapsort
Prime + Heap 简直神了 时间优化好多,顺便就把Heapsort给撸了一发 具体看图 Heapsort利用完全二叉树+大(小)顶锥的结构每次将锥定元素和锥最末尾的元素交换 同时大(小)顶锥元 ...
- 排序算法FOUR:堆排序HeapSort
/** *堆排序思路:O(nlogn) * 用最大堆,传入一个数组,先用数组建堆,维护堆的性质 * 再把第一个数与堆最后一个数调换,因为第一个数是最大的 * 把堆的大小减小一 * 再 在堆的大小上维护 ...
- 堆排序HeapSort
堆排序,顾名思义,是采用数据结构堆来进行排序的一种排序算法. 研究没有规律的堆,没有任何意义.特殊的堆有最大堆(父节点值大于等于左右字节点值),最小堆(父节点值小于等于子节点值).一般采用最大堆来进行 ...
- 算法分析-堆排序 HeapSort 优先级队列
堆排序的是集合了插入排序的单数组操作,又有归并排序的时间复杂度,完美的结合了2者的优点. 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关系之一时,称之为堆. 情形1:ki < ...
- 堆排序Heapsort的Java和C代码
Heapsort排序思路 将整个数组看作一个二叉树heap, 下标0为堆顶层, 下标1, 2为次顶层, 然后每层就是"3,4,5,6", "7, 8, 9, 10, 11 ...
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 堆排序算法 java 实现
堆排序算法 java 实现 白话经典算法系列之七 堆与堆排序 Java排序算法(三):堆排序 算法概念 堆排序(HeapSort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,可以利用数组的特 ...
- 排序 选择排序&&堆排序
选择排序&&堆排序 1.选择排序: 介绍:选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始 ...
- 排序算法——QuickSort、MergeSort、HeapSort(C++实现)
快速排序QuickSort template <class Item> void quickSort (Item a[], int l, int r) { if (r<=l) ret ...
随机推荐
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- java得到当前日期的前一天或后一天
public String getNextDay(String startdate) throws ParseException{ Date date = (new SimpleDateFormat( ...
- [leetcode]2. Add Two Numbers两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 如何在3GPP下载协议
以下载AT Commands协议为例. 1. 打开3GPP网址:http://www.3gpp.org/ 2. 打开Specification-Specification Numbering,如图.去 ...
- python积累二:中文乱码解决方法
根据网上提供的解决方法:添加#coding=utf-8或# -*- coding: utf-8 -*- #coding=utf-8 print "还不行?" 执行结果:还是乱码!: ...
- PHP+ajax实现二级联动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 二、putty的下载安装和基本使用方法教程
转载自:https://baijiahao.baidu.com/s?id=1597811787635071952&wfr=spider&for=pc PuTTY是一款开源(Open S ...
- centos 解压
解压 tar –xvf file.tar //解压 tar包 tar -xzvf file.tar.gz //解压tar.gz tar -xjvf file.tar.bz2 //解压 tar.bz ...
- RN与webview通讯
一.RN给webview发送信息 this.webview.postMessage(message) 二.监听从React Native发过来的消息: window.document.addEven ...