[Java data structure] Heap sort

1. Heap
concept

  • The heap is logically a complete binary tree;
  • The heap is physically stored in an array;
  • Satisfying that the value of any node is greater than the value of the node in its subtree, it is called a large heap, or a large root heap, or a maximum heap;
  • On the contrary, it is a small pile, or a small root pile, or a minimum pile
  • The basic function of the heap is to find the most valuable, the top element of the heap.

Operation-downward adjustment
Prerequisite: The left and right subtrees must already be a pile before adjustment can be made. The
adjustment process (take a small pile as an example):

  • index: the subscript representing the position to be adjusted
  • array: an array representing the storage heap
  • size: represents the number of heap elements in the array
  • left: represents the subscript of the left child of index
  • right: the subscript representing the right child of index
  • minIndex: save the subscript of the smallest child of index

Idea :
(Take small heap adjustment as an example)
1. If the
index is a leaf node, the adjustment ends (1) Determine whether the index position has children
(2) Because the heap is a complete binary tree, there must be no right children without a left child, so Determine whether there is a left child
(3) Because the storage structure of the heap is an array, to determine whether there is a left child is to determine whether the left child's subscript is out of bounds, that is, left >= size is out of bounds
2. Determine left or right, who is the youngest child of index minIndex
(1) If the right child does not exist, then minIndex = left
(2) Otherwise, compare the value of array[left] and array[right], choose the smaller one as minIndex
3. Compare the value of array[index] with array[minIndex ] Value, if array[index] <=array[minIndex], it meets the nature of the heap, and the adjustment ends
4. Otherwise, swap the values ​​of array[index] and array[min]
5. Then because of the nature of the heap at the minIndex position It may be destroyed, so consider minIndex as index, and continue to cycle through the above operations

to adjust the idea:
1. Determine whether index is the root node (index==0), and end if it is;
2. Find the parent node of index and compare it The value of and the value of the parent node, if array[Index]<=array[parentIndex], it ends; otherwise, exchange the values ​​of the two;
3. Let index be the value of the parent node, and continue the loop.
The overall idea is simpler, just compare with the parent node.
Reactor
building is also a cyclic adjustment process. First, ensure that it is a binary tree logically, and judge it according to the definition and nature of the heap to make it conform to the nature of the heap.

Heap application :
priority queue (heap):

public class MyPriorityQueue {
    
    
    private Integer[] array;
    private int size;
    
    public MyPriorityQueue(){
    
    
        array=new Integer[100];
        size=0;
        
    }
    //得到element下标的元素
    public Integer element(){
    
    
        if(size==0){
    
    
            throw new RuntimeException("空了");
        }
        return array[0];
    }

    //删除元素
    public Integer remove(){
    
    
        if(size==0){
    
    
            throw new RuntimeException("空的");
        }
        int e=array[0];
        array[0]=array[size-1];
        size--;
        adjustDown(0);
        return e;
    }
   public void adjustDown(int index){
    
    
        while(true){
    
    
            int leftIndex=2*index+1;
            if(array[leftIndex]>=size){
    
    
                break;
            }
            int minIndex=leftIndex;
            int rightIndex=leftIndex+1;
            if(rightIndex<size&&array[rightIndex]<array[leftIndex]){
    
    
                minIndex=rightIndex;
            }
            int t=array[index];
            array[index]=array[rightIndex];
            array[rightIndex]=t;

            index=minIndex;
        }
   }

    public void add(Integer e){
    
    
        array[size]=e;
        size++;
        adjustUp(size-1);
    }
    private void adjustUp(int index){
    
    
        while (true){
    
    
            if(index==0){
    
    
                break;
            }
            int parentIndex=(index-1)/2;
            if(array[parentIndex]<=array[index]){
    
    
                break;
            }
            int t=array[index];
            array[index]=array[parentIndex];
            array[parentIndex]=t;

            index=parentIndex;
        }

    }

    public static void main(String[] args) {
    
    
        MyPriorityQueue myPriorityQueue=new MyPriorityQueue();
        
        myPriorityQueue.add(1);
        myPriorityQueue.add(4);
        myPriorityQueue.add(3);
        myPriorityQueue.add(5);
        myPriorityQueue.add(6);
     }
 }

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/109141622