排序算法之堆排序

package sort;

public class HeapSort implements Sort {

    @Override
    public void downSort(int[] intArray) {
        // TODO Auto-generated method stub
        heapDownSort(intArray);
    }

    @Override
    public void upSort(int[] intArray) {
        // TODO Auto-generated method stub
        heapUpSort(intArray);
    }

    public static void printArray(int[] array) {
        System.out.print("{");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if (i < array.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("}");
    }

    public static void exchangeElements(int[] array, int index1, int index2) {
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }

    public static void heapUpSort(int[] array) {
        if (array == null || array.length <= 1) {
            return;
        }

        buildMaxHeap(array);

        for (int i = array.length - 1; i >= 1; i--) {
            HeapSort.exchangeElements(array, 0, i);

            maxHeap(array, i, 0);
        }
    }

    public static void heapDownSort(int[] array) {
        if (array == null || array.length <= 1) {
            return;
        }

        buildMinHeap(array);

        for (int i = array.length - 1; i >= 1; i--) {
            HeapSort.exchangeElements(array, 0, i);

            minHeap(array, i, 0);
        }
    }

    private static void buildMaxHeap(int[] array) {
        if (array == null || array.length <= 1) {
            return;
        }

        int half = array.length / 2;
        for (int i = half; i >= 0; i--) {
            maxHeap(array, array.length, i);
        }
    }

    private static void buildMinHeap(int[] array) {
        if (array == null || array.length <= 1) {
            return;
        }

        int half = array.length / 2;
        for (int i = half; i >= 0; i--) {
            minHeap(array, array.length, i);
        }
    }

    private static void maxHeap(int[] array, int heapSize, int index) {
        int left = index * 2 + 1;
        int right = index * 2 + 2;

        int largest = index;
        if (left < heapSize && array[left] > array[index]) {
            largest = left;
        }

        if (right < heapSize && array[right] > array[largest]) {
            largest = right;
        }

        if (index != largest) {
            HeapSort.exchangeElements(array, index, largest);

            maxHeap(array, heapSize, largest);
        }
    }

    private static void minHeap(int[] array, int heapSize, int index) {
        int left = index * 2 + 1;
        int right = index * 2 + 2;

        int largest = index;
        if (left < heapSize && array[left] < array[index]) {
            largest = left;
        }

        if (right < heapSize && array[right] < array[largest]) {
            largest = right;
        }

        if (index != largest) {
            HeapSort.exchangeElements(array, index, largest);

            maxHeap(array, heapSize, largest);
        }
    }

}

堆排序是分治算法,时间复杂度为O(n*logn).

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页