1.堆结构就是用数组实现的完全二叉树结构
2.完全二叉树中如果每棵子树的最大值都在顶部就是大根堆
3.完全二叉树中如果每棵子树的最小值都在顶部就是小根堆
4.堆结构的heapInsert和heapify操作
5.堆结构的增大和减少
6.优先级队列结构,就是堆结构
堆排序
先让整个数组都变成大根堆结构
把堆的最大值和堆末尾的值交换,然后减少堆的大小以后,再去调整堆,一直周而复始,时间复杂度O(N*logN)
堆的大小减小成0之后,排序完成
父节点与子节点的关系:左叶子节点:2*N+1
,右叶子节点:2*N+2
子节点与父节点的关系:父节点 (N-1)/2
private void heapSort(int[] arr){
if (null == arr || arr.length < 2){
return;
}
for (int i = 0; i < arr.length; i++) {
heapInsert(arr,i);
}
int size = arr.length;
swap(arr, 0, --size);
while (size != 0){
heapify(arr, 0, size);
swap(arr, 0, --size);
}
}
//从下往上,调成大根堆
private void heapInsert(int[] arr, int index){
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 size){
int left = 2 * index + 1;
while (left < size){
int largest = (left+1 < size) && arr[left]<arr[left+1] ? left+1 : left;
largest = arr[index] > arr[largest] ? index : largest;
//如果最大值是父节点处,那么就停止循环,因为接下来的结构都是之前调整好的
if (largest == index){
break;
}
swap(arr,largest,index);
index = largest;
left = 2 * index + 1;
}
}
private void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
堆排序扩展
1.【题目】
已知一个几乎有序的数组,几乎有序是指,如果把数组排好顺序的话,
每个元素移动的距离可以不超过K,并且k相对于数组来说比较小,
请选择一个合适的算法进行排序
【思路】优先级队列是用堆实现的,可以直接利用优先级队列,把优先级队列直接当做堆排序就好
private void sortedArrDistanceLessK(int[] arr, int k){
PriorityQueue<Integer> heap = new PriorityQueue<>();
int index = 0;
for (; index < Math.min(k, arr.length); index++) {
heap.add(arr[index]);
}
int i = 0;
for (; index < arr.length; i++, index++) {
arr[i] = heap.poll();
heap.add(arr[index]);
}
while (!heap.isEmpty()){
arr[i++] = heap.poll();
}
}
2.【题目】
在一个数据流中,随时获取中位数
【思路】准备一个大根堆和一个小根堆,第一个数字放入大根堆,以后每个数字判断是否小于大根堆的堆顶元素,小于,放大根堆,大于,放小根堆,同时比较两个根堆的元素个数差,不能超过1,超过1,元素多的取堆顶元素进元素少的堆。全部元素入堆后,根据总元素个数,得出中位数。
public class MadianQuick {
public static class MedianHolder {
private PriorityQueue<Integer> minHeapQueue = new PriorityQueue<>(new MinHeapComparator());
private PriorityQueue<Integer> maxHeapQueue = new PriorityQueue<>(new MaxHeapComparator());
public void addNum(int num){
if (maxHeapQueue.isEmpty() || num < maxHeapQueue.peek()){
maxHeapQueue.add(num);
} else {
minHeapQueue.add(num);
}
modifyTwoHeapsSize();
}
private void modifyTwoHeapsSize() {
if(minHeapQueue.size() == maxHeapQueue.size() + 2){
maxHeapQueue.add(minHeapQueue.poll());
}
if (maxHeapQueue.size() == minHeapQueue.size() + 2){
minHeapQueue.add(maxHeapQueue.poll());
}
}
private Integer getMedian(){
if (maxHeapQueue.size() + minHeapQueue.size() == 0){
return null;
}
Integer maxHeapHead = maxHeapQueue.peek();
Integer minHeadHead = minHeapQueue.peek();
if (maxHeapQueue.size() == minHeapQueue.size()){
return (maxHeapHead + minHeadHead) / 2;
}
return maxHeapQueue.size() > minHeapQueue.size() ? maxHeapHead : minHeadHead;
}
}
public static class MinHeapComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2 > 0 ? 1 : -1;
}
}
public static class MaxHeapComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1 > 0 ? 1 : -1;
}
}
public static int getMedianOfArray(int[] arr){
int[] copy = Arrays.copyOf(arr, arr.length);
Arrays.sort(copy);
int mid = (copy.length-1)/2;
if ((copy.length & 1) == 0){
return (copy[mid]+copy[mid+1])/2;
} else {
return copy[mid];
}
}
public static void main(String[] args) {
MedianHolder medianHolder = new MedianHolder();
int[] arr = {5,3,2,7,9,8,6,4,3,1,7};
for (int i : arr) {
medianHolder.addNum(i);
}
int medianOfArray = getMedianOfArray(arr);
Integer median = medianHolder.getMedian();
System.out.println(median);
System.out.println(medianOfArray);
}
}