堆-B-堆排序
2017-02-23 12:45
253 查看
package j_heap.B_heapSort;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 堆排序
* 数组显示的时候大小不能为数组个数,因为堆排序后,数组个数清0,将无法显示。而大小则不会
* 缺点:没有屏蔽掉Heap类对堆的操作(insertAt()方法),数组和堆结构联系紧密
* @author Administrator
*
*/
public class HeapApp {
public static void main(String[] args) throws Exception {
int size, j;
System.out.print("请输入数组大小:");
size = getInt();
Heap theHeap = new Heap(size);
// 随机填入数字
for (j = 0; j < size; j++) {
int random = (int) (Math.random() * 100);
Node newNode = new Node(random);
theHeap.insert(j, newNode);
theHeap.incrementSize();
}
// 显示随机数组
System.out.print("Random: ");
theHeap.displayArray();
// 有子节点的节点放在对应的位置上
for (j = size / 2 - 1; j >= 0; j--) {
theHeap.trickleDown(j);
}
// 数组+树显示
System.out.print("Heap: ");
theHeap.displayArray();
theHeap.displayHeap();
for (j = size - 1; j >= 0; j--) {
Node biggestNode = theHeap.remove();
theHeap.insert(j, biggestNode);
}
System.out.print("Sorted: ");
theHeap.displayArray();
}
private static String getString() throws IOException {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
}
private static int getInt() throws NumberFormatException, IOException {
return Integer.parseInt(getString());
}
}package j_heap.B_heapSort;
public class Heap {
private Node[] heapArray;
private int maxSize;
private int currentSize;
public Heap(int size) {
maxSize = size;
heapArray = new Node[maxSize];
currentSize = 0;
}
public Node remove() {
Node root = heapArray[0];
heapArray[0] = heapArray[--currentSize];
trickleDown(0);
return root;
}
/**
* 向下筛选 把index索引对应的数组值放在对应的位置上
*
* @param index
*/
public void trickleDown(int index) {
int largerChild;
Node top = heapArray[index];
while (index < currentSize / 2) {
int leftChild = 2 * index + 1;
int rightChild = leftChild + 1;
if (rightChild < currentSize
&& heapArray[rightChild].getiData() > heapArray[leftChild]
.getiData()) {
largerChild = rightChild;
} else {
largerChild = leftChild;
}
if (top.getiData() >= heapArray[largerChild].getiData())
break;
heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}
/**
* 二叉树显示
*/
public void displayHeap() {
int nBlanks = 32;
int itemsPerRow = 1;
int column = 0;
int j = -1;
while (j++ < currentSize - 1) {// 1:currentSize>0
// 外层空白
if (column == 0) {
for (int i = 0; i < nBlanks; i++) {
System.out.print(' ');
}
}
System.out.print(heapArray[j].getiData());
// 2:if(++j==currentSize)break;
// 控制换行和中间空白
if (++column == itemsPerRow) {
nBlanks /= 2;
itemsPerRow *= 2;
column = 0;
// 换第二层
System.out.println();
} else {// 中间空白
for (int i = 0; i < nBlanks * 2 - 2; i++) {
System.out.print(' ');
}
}
}
System.out.println();
}
/**
* 数组显示
*/
public void displayArray() {
//大小不能为数组个数,因为堆排序后,数组个数清0,将无法显示。而大小则不会
for (int i = 0; i < maxSize; i++) {
System.out.print(heapArray[i].getiData() + " ");
}
System.out.println();
}
/**
* 对应索引插入节点
*
* @param index
* @param newNode
*/
public void insert(int index, Node newNode) {
heapArray[index] = newNode;
}
/**
* 增长个数
*/
public void incrementSize() {
currentSize++;
}
}package j_heap.B_heapSort;
public class Node {
private int iData;
public Node(int iData) {
this.iData = iData;
}
public int getiData() {
return iData;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 堆排序
* 数组显示的时候大小不能为数组个数,因为堆排序后,数组个数清0,将无法显示。而大小则不会
* 缺点:没有屏蔽掉Heap类对堆的操作(insertAt()方法),数组和堆结构联系紧密
* @author Administrator
*
*/
public class HeapApp {
public static void main(String[] args) throws Exception {
int size, j;
System.out.print("请输入数组大小:");
size = getInt();
Heap theHeap = new Heap(size);
// 随机填入数字
for (j = 0; j < size; j++) {
int random = (int) (Math.random() * 100);
Node newNode = new Node(random);
theHeap.insert(j, newNode);
theHeap.incrementSize();
}
// 显示随机数组
System.out.print("Random: ");
theHeap.displayArray();
// 有子节点的节点放在对应的位置上
for (j = size / 2 - 1; j >= 0; j--) {
theHeap.trickleDown(j);
}
// 数组+树显示
System.out.print("Heap: ");
theHeap.displayArray();
theHeap.displayHeap();
for (j = size - 1; j >= 0; j--) {
Node biggestNode = theHeap.remove();
theHeap.insert(j, biggestNode);
}
System.out.print("Sorted: ");
theHeap.displayArray();
}
private static String getString() throws IOException {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
}
private static int getInt() throws NumberFormatException, IOException {
return Integer.parseInt(getString());
}
}package j_heap.B_heapSort;
public class Heap {
private Node[] heapArray;
private int maxSize;
private int currentSize;
public Heap(int size) {
maxSize = size;
heapArray = new Node[maxSize];
currentSize = 0;
}
public Node remove() {
Node root = heapArray[0];
heapArray[0] = heapArray[--currentSize];
trickleDown(0);
return root;
}
/**
* 向下筛选 把index索引对应的数组值放在对应的位置上
*
* @param index
*/
public void trickleDown(int index) {
int largerChild;
Node top = heapArray[index];
while (index < currentSize / 2) {
int leftChild = 2 * index + 1;
int rightChild = leftChild + 1;
if (rightChild < currentSize
&& heapArray[rightChild].getiData() > heapArray[leftChild]
.getiData()) {
largerChild = rightChild;
} else {
largerChild = leftChild;
}
if (top.getiData() >= heapArray[largerChild].getiData())
break;
heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}
/**
* 二叉树显示
*/
public void displayHeap() {
int nBlanks = 32;
int itemsPerRow = 1;
int column = 0;
int j = -1;
while (j++ < currentSize - 1) {// 1:currentSize>0
// 外层空白
if (column == 0) {
for (int i = 0; i < nBlanks; i++) {
System.out.print(' ');
}
}
System.out.print(heapArray[j].getiData());
// 2:if(++j==currentSize)break;
// 控制换行和中间空白
if (++column == itemsPerRow) {
nBlanks /= 2;
itemsPerRow *= 2;
column = 0;
// 换第二层
System.out.println();
} else {// 中间空白
for (int i = 0; i < nBlanks * 2 - 2; i++) {
System.out.print(' ');
}
}
}
System.out.println();
}
/**
* 数组显示
*/
public void displayArray() {
//大小不能为数组个数,因为堆排序后,数组个数清0,将无法显示。而大小则不会
for (int i = 0; i < maxSize; i++) {
System.out.print(heapArray[i].getiData() + " ");
}
System.out.println();
}
/**
* 对应索引插入节点
*
* @param index
* @param newNode
*/
public void insert(int index, Node newNode) {
heapArray[index] = newNode;
}
/**
* 增长个数
*/
public void incrementSize() {
currentSize++;
}
}package j_heap.B_heapSort;
public class Node {
private int iData;
public Node(int iData) {
this.iData = iData;
}
public int getiData() {
return iData;
}
}
相关文章推荐
- 堆和堆排序
- 10种算法原理(冒泡排序,选择排序,快速排序,堆排序,希尔排序,桶排序等)
- STL算法,堆排序
- 白话经典算法系列之七 堆与堆排序
- 常见排序算法总结与实现(冒泡、插入、选择、希尔、堆排序、归并、快排)
- 堆排序java实现
- 6种排序算法及其比较 简单选择排序,堆排序,简单插入排序,希尔排序,冒泡排序,快速排序,归并排序
- 堆排序 Heap Sort
- 堆排序
- 堆排序原理及实现
- java 堆排序
- 选择排序---简单选择排序 堆排序
- 排序算法08:优先队列与堆排序
- 最大堆排序
- 数据结构基础加强之最小堆的实现与堆排序
- 排序--堆排序
- hdu 1280 堆排序
- c++实现最大堆建立(链表结构)和堆排序
- 堆排序详解
- 排序 堆排序