11/30-Heap实现、TopK问题、堆排序

Heap.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef int HPDataType;

typedef struct Heap
{
    HPDataType* a;
    int size;
    int capacity;
}HP;

void HeapInit(HP* hp);
void HeapDestroy(HP* hp);
void HeapPush(HP* hp, HPDataType x);
void HeapPop(HP* hp);
HPDataType HeapTop(HP* hp);
void HeapPrint(HP* hp);
void AdjustUp(int* a, int child);
void AdjustDown(int* a, int n, int parent);

void Swap(HPDataType* px, HPDataType* py);
bool HeapEmpty(HP* hp);
int HeapSize(HP* hp);

Heap.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Heap.h"

// 堆的创建
void HeapInit(HP* hp)
{
    assert(hp);

    hp->a = NULL;
    hp->size = hp->capacity = 0;
}

// 堆的销毁
void HeapDestroy(HP* hp)
{
    assert(hp);

    free(hp->a);
    hp->capacity = hp->size = 0;
}

// 打印查看
void HeapPrint(HP* hp)
{
    for (int i = 0; i < hp->size; ++i)
    {
        printf("%d ", hp->a[i]);
    }
    printf("\n");
}

// 堆的判空
bool HeapEmpty(HP* hp)
{
    assert(hp);

    return hp->size == 0;
}

// 堆的数据个数
int HeapSize(HP* hp)
{
    assert(hp);

    return hp->size;
}

// 交换数据
void Swap(HPDataType* px, HPDataType* py)
{
    HPDataType tmp = *px;
    *px = *py;
    *py = tmp;
}

// 向上调整算法
void AdjustUp(int* a, int child)// 调整位置下标
{
    assert(a);

    int parent = (child - 1) / 2;
    while (child > 0)// while(parent >= 0)
    {
        if (a[child] > a[parent])
        {
            Swap(&a[child], &a[parent]);// 交换数据

            child = parent;
            parent = (child - 1) / 2;
        }
        else
        {
            break;
        }
    }
}

// 向下调整算法
void AdjustDown(int* a, int n, int parent)
{
    int child = parent * 2 + 1;// 默认选中左孩子
    while (child < n)
    {
        // 选出左右孩子中小的那一个
        if (child + 1 < n && a[child + 1] > a[child])
        {
            ++child;
        }
        // 如果小的孩子小于父亲,则交换;并继续向下调整
        if (a[child] > a[parent])
        {
            Swap(&a[child], &a[parent]);
            parent = child;
            child = parent * 2 + 1;
        }
        else
        {
            break;
        }
    }
}

// 堆的插入
void HeapPush(HP* hp, HPDataType x)
{
    assert(hp);
    // 判断增容
    if (hp->size == hp->capacity)
    {
        size_t newCapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
        HPDataType* tmp = realloc(hp->a, sizeof(HPDataType) * newCapacity);
        if (tmp == NULL)
        {
            printf("realloc fail");
            exit(-1);
        }

        hp->a = tmp;
        hp->capacity = newCapacity;
    }
    // 插入数据
    hp->a[hp->size] = x;
    hp->size++;
    // 调整数据
    AdjustUp(hp->a, hp->size - 1);
}

// 堆的删除 - 删除堆顶的数据 - 向下调整
void HeapPop(HP* hp)
{
    assert(hp);
    assert(!HeapEmpty(hp));

    Swap(&hp->a[0], &hp->a[hp->size - 1]);// 堆顶数据和最后一个数据交换
    hp->size--;

    AdjustDown(hp ->a, hp->size, 0);
}

// 获取堆顶元素
HPDataType HeapTop(HP* hp)
{
    assert(hp);
    assert(!HeapEmpty(&hp));

    return hp->a[0];
}

Test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Heap.h"
// 大、小堆的实现
void TestHeap()
{
    int a[] = { 70,56,30,25,15,10,75 };
    HP hp;
    HeapInit(&hp);
    for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
    {
        HeapPush(&hp, a[i]);
    }

    HeapPrint(&hp);

    HeapPop(&hp);
    HeapPrint(&hp);

    HeapPop(&hp);
    HeapPrint(&hp);

    HeapPop(&hp);
    HeapPrint(&hp);

    HeapPop(&hp);
    HeapPrint(&hp);

    HeapDestroy(&hp);
}

// 在N个数中找出最大的前K个 / 在N个数中找出最小的前K个
void PrintTopK(int* a, int n, int k)
{
    HP hp;
    HeapInit(&hp);
    // 1. 建堆--用a中前K个元素建堆
    for (int i = 0; i < k; ++i)
    {
        HeapPush(&hp, a[i]);
    }
    // 2. 将剩余N-K个元素依次与堆顶元素比较,大于堆顶元素则替换进堆
    for (int i = k; i < n; ++i)
    {
        if (a[i] > HeapTop(&hp))
        {
            // 1.
            HeapPop(&hp);
            HeapPush(&hp, a[i]);
             2.
            //hp.a[0] = a[i];
            //AdjustDown(hp.a, hp.size, 0);
        }
    }
    HeapPrint(&hp);

    HeapDestroy(&hp);
}

// Topk问题求解
void TestTopk()
{
    int n = 10000;
    int* a = (int*)malloc(sizeof(int) * n);
    srand(time(0));
    for (size_t i = 0; i < n; ++i)
    {
        a[i] = rand() % 1000000;
    }
    // 再设置10个比100w大的数
    a[5] = 1000000 + 1;
    a[1231] = 1000000 + 2;
    a[531] = 1000000 + 3;
    a[511] = 1000000 + 4;
    a[15] = 1000000 + 5;
    a[35] = 1000000 + 6;
    a[999] = 1000000 + 7;
    a[76] = 1000000 + 8;
    a[423] = 1000000 + 9;
    a[3144] = 1000000 + 10;
    PrintTopK(a, n, 10);
}

// 堆排序 - 升序 - 空间复杂度O(N) - 要求优化到O(1) -> 不能用Heap
void HeapSort_1(int* a, int n)
{
    HP hp;
    HeapInit(&hp);
    // 建立一个N个小堆
    for (int i = 0; i < n; ++i)
    {
        HeapPush(&hp, a[i]);
    }

    // Pop N 次
    for (int i = 0; i < n; ++i)
    {
        a[i] = HeapTop(&hp);
        HeapPop(&hp);
    }

    HeapDestroy(&hp);
}

// 
void TestHeapSort_1()
{
    int a[] = { 70,56,30,25,15,10,75 };
    for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
    {
        printf("%d ", a[i]);
    }
    printf("\n");

    HeapSort_1(a, sizeof(a) / sizeof(a[0]));

    for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}

// 堆排序
void HeapSort(int* a, int n)
{
    // 把a构建成小堆(后续还要使用向上调整,费力)
     方法1:a[]的第一个数先看做堆,后面的数据依次加入堆,通过向上调整构建堆
    //for (int i = 1; i < n; ++i)
    //{
    //    AdjustUp(a, i);
    //}

     方法2:叶子所在的子树不需要调,从倒着数第一个非叶子节点的子树开始调(最后一个节点的父亲)
    //for (int i = (n - 1 - 1) / 2; i >= 0; --i)
    //{
    //    AdjustDown(a, n, i);
    //}


    // 堆排序 - 排升序,建大堆;排降序,建小堆
    // 1、排大堆[O(N)]
    for (int i = (n - 1 - 1) / 2; i >= 0; --i)
    {
        AdjustDown(a, n, i);
    }

    // 2、依次选数,调堆[O(N*logN)]
    for (int end = n - 1; end > 0; --end)
    {
        Swap(&a[end], &a[0]);

        // 再调堆,选出次小的数
        AdjustDown(a, end, 0);
    }
}

void TestHeapSort()
{
    int a[] = { 70,56,30,25,15,10,75,33,50,69 };
    for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
    {
        printf("%d ", a[i]);
    }
    printf("\n");

    HeapSort(a, sizeof(a) / sizeof(a[0]));

    for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}

int main()
{
    //TestHeap();
    //TestTopk();
    //TestHeapSort_1();
    TestHeapSort();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值