一、堆
1)堆结构就是用数组实现的完全二叉树结构
2)完全二叉树中如果每棵子树的最大值都在顶部就是大根堆
3)完全二叉树中如果每棵子树的最小值都在顶部就是小根堆
4)左孩子2i + 1 , 右孩子2i + 2 , 父节点 (i - 1)/2
5)size变量控制大小
package com.lzf2.class05;
/**
* 大根堆
*/
public class Heap {
private int[] heap;
private int heapSize;
public Heap(int capacity) {
heap = new int[capacity];
heapSize = 0;
}
public boolean isEmpty() {
return heapSize == 0;
}
public boolean isFull() {
return heapSize == heap.length;
}
public void push(int value){
if(isFull()){
throw new RuntimeException("heap is full");
}
heap[heapSize] = value;
heapInsert(heap,heapSize++);
}
public int pop(){
int ans = heap[0];
swap(heap,0,--heapSize);
heapify(heap,0,heapSize);
return ans;
}
private void heapInsert(int[] arr, int index) {
//index == 0 时 index - 1)/2为0
while (arr[index] > arr[(index - 1) / 2]) {
//交换
swap(arr, index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
private void heapify(int[] arr, int index,int heapSize) {
int left = index * 2 + 1;
while (left < heapSize) {
//如果有孩子,找到孩子中大的那一个
int largest = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left;
largest = arr[largest] > arr[index] ? largest : index;
if (largest == index) {
break;
}
//交换
swap(arr, largest, index);
index = largest;
left = index * 2 + 1;
}
}
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
二、堆排序
arr[0…N]调成大根堆
0位置和N - 1位置交换,大根堆heapSize–
重新调整大根堆
heapSize > 0时,重复2,3步骤。否则结束
package com.lzf2.class05;
import java.util.Arrays;
import java.util.PriorityQueue;
/**
* 堆排序
*/
public class HeapSort {
public static void sort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
//1.先把数组弄成大根堆
for (int i = 0; i < arr.length; i++) {
heapInsert(arr,i);
}
//2.0位置和最后一个数交换,减小大根堆,重新调整大根堆
int heapSize = arr.length;
while (heapSize > 0){
swap(arr,0,--heapSize);
heapify(arr,0,heapSize);
}
}
private static void heapInsert(int[] arr, int index) {
//index == 0 时 index - 1)/2为0
while (arr[index] > arr[(index - 1) / 2]) {
//交换
swap(arr, index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
private static void heapify(int[] arr, int index,int heapSize) {
int left = index * 2 + 1;
while (left < heapSize) {
//如果有孩子,找到孩子中大的那一个
int largest = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left;
largest = arr[largest] > arr[index] ? largest : index;
if (largest == index) {
break;
}
//交换
swap(arr, largest, index);
index = largest;
left = index * 2 + 1;
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// for test
public static void comparator(int[] arr) {
Arrays.sort(arr);
}
// for test
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
return arr;
}
// for test
public static int[] copyArray(int[] arr) {
if (arr == null) {
return null;
}
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
// for test
public static boolean isEqual(int[] arr1, int[] arr2) {
if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
return false;
}
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
// for test
public static void printArray(int[] arr) {
if (arr == null) {
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// for test
public static void main(String[] args) {
// 默认小根堆
PriorityQueue<Integer> heap = new PriorityQueue<>();
heap.add(6);
heap.add(8);
heap.add(0);
heap.add(2);
heap.add(9);
heap.add(1);
while (!heap.isEmpty()) {
System.out.println(heap.poll());
}
int testTime = 500000;
int maxSize = 100;
int maxValue = 100;
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr1 = generateRandomArray(maxSize, maxValue);
int[] arr2 = copyArray(arr1);
sort(arr1);
comparator(arr2);
if (!isEqual(arr1, arr2)) {
succeed = false;
break;
}
}
System.out.println(succeed ? "Nice!" : "Fucking fucked!");
int[] arr = generateRandomArray(maxSize, maxValue);
printArray(arr);
sort(arr);
printArray(arr);
}
}