007 sorting algorithm - HEAPSORT

I. Overview

  HEAPSORT (English: Heapsort) refers to a sorting algorithm such a data structure designed for use heap. A stack is nearly complete binary tree structure, properties and satisfy deposited: key or index that is a child node is always less than (or greater than) its parent node.

Sort method Time complexity (average) Time complexity (worst) Time complexity (best) Space complexity stability
Heapsort $ O (nlogn) $ O (nlogn) O (nlogn) O (1) Unstable

  In the data structure of the stack, the stack is always the maximum value of the root node (heap priority queue, then the minimum value of the root node in the stack). Stack defines the following operations:

  • Adjusting the maximum heap (Max Heapify): The pile end child node adjusted, so that the child node is always less than the parent
  • Creating maximum heap (Build Max Heap): all data stack reordering
  • Heapsort (HeapSort): remove the root node in the first position data and make adjustments largest heap of recursive algorithm

1.1 Description

   

  Constantly large root stack top of the heap and stack element exchange the last element, then the reconstruction process large root heap. To be larger than the root of the heap sort column constructed with variable heapSize to control the size of the heap, and then continuously heapify, until heapSize zero. In the process of heapify's is the first comparison sub-node size, and then compare parent and child nodes of the size, if finally found the parent node is larger than a child node, then jump out heapify cycle, otherwise the child nodes of the parent node exchange, and found the child node the left child, and then repeat the process of appeal, until the children left the position left is greater than heapSize, ending heapify cycle.

1.2, code implementation

public class HeapSort {
    public static void heapSort(int [] arr){
        if(arr == null || arr.length < 2){
            return;
        }
        for(int i = 0; i< arr.length; i++){
            heapInsert(arr,i);
        }

        int heapSize = arr.length;
        swap(arr, 0, --heapSize);
        while(heapSize > 0){
            heapify(arr, 0, heapSize);
            swap(arr, 0, --heapSize);
        }
    }

    private static void heapInsert(int[] arr, int index){
        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 tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

Code address: Address  algorithm-001-sort of the HeapSort  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

send to

Guess you like

Origin www.cnblogs.com/bjlhx/p/10953791.html