堆排序

2019-08-08  本文已影响0人  暑水

定义:

数据结构:

C 语言
struct MaxHeap
{
    EType *heap; //存放数据的空间,下标从1开始存储数据,下标为0的作为工作空间,存储临时数据。
    int MaxSize; //MaxSize是存放数据元素空间的大小
    int HeapSize; //HeapSize是数据元素的个数
};
MaxHeap H;

初始化堆

  public static void MaxHeapInit(int[] array) {
        for(int i = array.length/2; i > 0; --i){
            array[0] = array[i];//堆值存储从1开始,使用array[0]暂存
            int child = 2*i;
            while(child < array.length){
                if (child < array.length - 1 && array[child] < array[child+1])
                    child++;
                if(array[0] > array[child])
                    break;
                array[child/2] = array[child];
                child = child*2;
            }
            array[child/2] = array[0];
        }
    }

最大堆中插入结点

C 语言源码
void MaxHeapInsert(MaxHeap &H, EType &x)
{
    if(H.HeapSize==H.MaxSize) return false;    
    int i=++H.HeapSize;
    while(i!=1&&x>H.heap[i/2]){
        H.heap[i]=H.heap[i/2];
        i/=2;
    }
    H.heap[i]=x;
    return true;
}

最大堆中删除结点

实战:

升序排序使用大顶堆,降序排序使小顶堆;
public class Main {
    public static void main(String[] args) {
        int[] array = { 0, 20, 70, 30, 10, 60, 50, 30, 90, 100 };
        String aString = Arrays.toString(array);
        System.out.println("排序前   " + aString);
        heapSort(array);
        aString = Arrays.toString(array);
        System.out.println("排序后  " + aString);
    }

    public static void heapSort(int[] array) {
        for (int i = array.length / 2; i > 0; i--) { // 建堆
            heapAdjust(array, i, array.length - 1);
        }

        for (int i = array.length - 1; i > 1; i--) {
            swap(array, 1, i); //将最大值交换到最后
            heapAdjust(array, 1, i - 1);//将剩余数组更新堆
        }
    }

    public static void heapAdjust(int[] array, int small, int large) {
        int temp = array[small];
        for (int j = small * 2; j <= large; j = j * 2) {
            if (j < large && array[j] < array[j + 1]) {
                j++;
            }
            if (temp >= array[j])
                break;
            array[small] = array[j];
            small = j;
        }
        array[small] = temp;
    }

    public static void swap(int[] array, int small, int large) {
        int temp = array[small];
        array[small] = array[large];
        array[large] = temp;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读