堆排序

废话不多说先上代码

void heapSort(int arr[], int length) {
    int *heap = heapBuild(arr, length);
    for(int i = 0; i < length; ++i) {
        arr[i] = heapPop(heap, length);
    }

    return;
}
int* heapBuild(int arr[], int length) {
    int *heap = (int*)calloc(length + 1, sizeof(int));
    int middle = length >> 1;
    for(int i = middle; i > 0; --i) {
        heapfiy(heap, i, length);
    }

    return heap;
}
void heapfiy(int *heap, int index, int length) {
    int swap = index;
    int temp;
    while(1) {
        if(index * 2 <= length && heap[index * 2] < heap[swap])
            swap = index * 2;
        if(index * 2 + 1 <= length && heap[index * 2 + 1] < heap[swap])
            swap = index * 2 + 1;
        if(index == swap)
            break;
        temp = heap[index];
        heap[index] = heap[swap];
        heap[swap] = temp;
        index = swap;
    }

    return;
}
int heapPop(int *heap, int *length) {
    int result = heap[1];
    heap[1] = heap[*length];
    (*length)--;
    heapfiy(heap, 1, *length);

    return result;
}

时间复杂度

O(n * log n)

空间复杂度

O(1) 原地排序,注意我这里写的代码不是原地排序。

稳定排序

不是稳定排序,因为弹出堆顶元素要和最后一个元素互换,保证二叉树的完全性。

算法核心思想

将待排序数组构建为二叉堆,不断弹出堆顶元素。
关于什么是二叉堆我就不详细说了,以后有时间再写。

堆排序的实现

将待排序数组构建成二叉堆

//堆排序
void heapSort(int arr[], int length) {
    int *heap = heapBuild(arr, length);
}
//建堆函数
int* heapBuild(int arr[], int length) {
    //为了方便计算数的下标 我们申请一个比length大的一的空间
    int *heap = (int*)calloc(length + 1, sizeof(int));
     //我们只需对非子节点进行堆化;
    int middle = length >> 1;
    for(int i = middle; i > 0; --i) {
        heapfiy(heap, i, length);
    }

    return heap;
}
//堆化过程
void heapfiy(int *heap, int index, int length) {
    int swap = index;
    int temp;
    while(1) {
        //如果左节点小于当前节点就交换
        if(index * 2 <= length && heap[index * 2] < heap[swap])
            swap = index * 2;
        //如果右节点小于当前节点 或 小于右节点且小于当前节点就交换
        if(index * 2 + 1 <= length && heap[index * 2 + 1] < heap[swap])
            swap = index * 2 + 1;
        //终止条件 当前节点大于子节点
        if(index == swap)
            break;
        temp = heap[index];
        heap[index] = heap[swap];
        heap[swap] = temp;
        index = swap;
    }

    return;
}

弹出对顶元素并赋值

//堆排序
void heapSort(int arr[], int length) {
    int *heap = heapBuild(arr, length);
    for(int i = 0; i < length; ++i) {
        arr[i] = heapPop(heap, length);
    }

    return;
}
//建堆函数
int* heapBuild(int arr[], int length) {
    //为了方便计算数的下标 我们申请一个比length大的一的空间
    int *heap = (int*)calloc(length + 1, sizeof(int));
     //我们只需对非子节点进行堆化;
    int middle = length >> 1;
    for(int i = middle; i > 0; --i) {
        heapfiy(heap, i, length);
    }

    return heap;
}
//堆化过程
void heapfiy(int *heap, int index, int length) {
    int swap = index;
    int temp;
    while(1) {
        //如果左节点小于当前节点就交换
        if(index * 2 <= length && heap[index * 2] < heap[swap])
            swap = index * 2;
        //如果右节点小于当前节点 或 小于右节点且小于当前节点就交换
        if(index * 2 + 1 <= length && heap[index * 2 + 1] < heap[swap])
            swap = index * 2 + 1;
        //终止条件 当前节点大于子节点
        if(index == swap)
            break;
        temp = heap[index];
        heap[index] = heap[swap];
        heap[swap] = temp;
        index = swap;
    }

    return;
}
//弹出堆顶元素
int heapPop(int *heap, int *length) {
    int result = heap[1];
    heap[1] = heap[*length];
    (*length)--;
    heapfiy(heap, 1, *length);

    return result;
}

堆排序到这就结束了,动手写一遍会更容易理解~

堆排序,堆排序你必须理解什么是堆,再谈排序。以后有时间我会写写这类数据结构的。
都看到这了,点个赞再走啊~。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是堆排序? 摘自漫画算法: 记得二叉堆的特性是什么吗? 最大堆的堆顶是整个堆中最大的元素。 最小堆的堆顶是整个...
    花逝97阅读 168评论 0 1
  • 二叉堆其实就是一棵堆有序的二叉树 开篇 本篇文章主要讲什么 此文是排序算法系列文章的倒数第三篇,因此本文的主要意图...
    xiaofei_dev阅读 315评论 0 0
  • 堆(大顶堆)的概念 堆是一种特殊的二叉树,大顶堆就是根节点为最大值的堆,它具有如下的特点: 堆是完全二叉树 堆常用...
    invincine阅读 1,359评论 0 5
  • 堆排序是利用二叉堆的自调整特性将数组变为有序序列的排序方法二叉堆的特性: 最大堆的堆顶是整个堆中的最大元素。 最小...
    吕艳凯阅读 125评论 0 0
  • 第十五章 Caché 算法与数据结构 堆排序 二叉堆特性 最大堆的堆顶是整个堆中的最大元素。 最小堆的堆顶是整个堆...
    Cache技术分享阅读 268评论 0 0