public static void swap(int[] arr , int i , int j){ arr[i] =arr[i] ^arr[j]; arr[j] =arr[i] ^arr[j]; arr[i] =arr[i] ^arr[j]; } //建一个大根堆 public static class MyMaxHeap{ private int[] heap; private final int limit; private int heapSize; public MyMaxHeap(int limit){ heap = new int[limit]; this.limit = limit; heapSize =0; } public boolean isEmpty(){ return heapSize==0; } public boolean isFull(){ return heapSize ==limit; } public void push(int value){ if(heapSize == limit){ 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){ 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; } } }标签:大根堆,arr,index,实现,int,heapSize,heap,left From: https://blog.csdn.net/2401_83010439/article/details/140435109