随笔 - 65  文章 - 0  评论 - 21  阅读 - 32万

【CLRS】《算法导论》读书笔记(一):堆排序(Heapsort)

堆排序(Heapsort)

维基百科:http://en.wikipedia.org/wiki/Heapsort

时间复杂度:O(n log n)

示例:

[6, 5, 3, 1, 8, 7, 2, 4]

 

1、堆(Heap)

维基百科:http://en.wikipedia.org/wiki/Heap_(data_structure)

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: If A is a parent node of B then key(A) is ordered with respect to key(B) with the same ordering applying across the heap.

 

PARENT(i)

  return i / 2

LEFT(i)

  return 2 * i

RIGHT(i)

  2 * i + 1

 

2、Heapify

最大堆为例,伪代码:

MAX-HEAPIFY(A, i)

  l = LIFT(i)

  r = RIGHT(i)

  if l <= A.heapsize and A[l] > A[i]

    largest = l

  else largest = i

  if r <= A.heapsize and A[r] > A[largest]

    largest = r

  if largest != i

    exchage A[i] with A[largest]

    MAX-HEAPIFY(A, largest)

 

3、Build Heap

最大堆为例,伪代码:

BUILD-MAX-HEAP(A)

  A.heap-size = A.length

  for A.length / 2 downto 1

    MAX-HEAPIFY(A, i)

 

4、Heapsort

最大堆为例,伪代码:

HEAPSORT(A)

  BUILD-MAX-HEAP(A)

  for i = A.length downto 2

    exchange A[1] with A[i]

    A.heap-size = A.heap-size - 1

    MAX-HEAPIFY(A, 1)

 

5、Priority Queue

最大堆为例,伪代码:

HEAP-MAXIMUM(A)

  return A[1]

 

HEAP-EXTRACT-MAX(A)

  if A.heap-size < 1

    error "heap underflow"

  max = A[1]

  A[1] = A[A.heap-size]

  A.heap-size = A.heap-size - 1

  MAX-HEAPIFY(A, 1)

  return max

 

HEAP-INCREASE-KEY(A, i, key)

  if key < A[i]

    error "new key is smaller than current key"

  A[i] = key

  while i > 1 and A[PARENT(i)] < A[i] 

    exchange A[i] with A[PARENT(i)]

    i = PARENT(i)

 

MAX-HEAP-INSERT(A, key)

  A.heap-size = A.heap-size + 1

  A[A.heap-size] = - INFINITY

  HEAP-INCREASE-KEY(A, A.heap-size, key)

 

C语言实现

heap.h

复制代码
typedef struct {
    int *array;
    int length;
    int heap_size;
} Heap;

int heap_parent(int index);
int heap_left_child(int index);
int heap_right_child(int index);
复制代码

 

heap.c

复制代码
int heap_parent(int index) {
    return (index - 1) / 2;
}

int heap_left_child(int index) {
    return 2 * index + 1;
}

int heap_right_child(int index) {
    return 2 * (index + 1);
}
复制代码

 

heap_sort.h

#import "heap.h"

void max_heap_sort(Heap heap);

 

heap_sort.c

复制代码
#import "heap_sort.h"

void exchange(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void max_heapify(Heap heap, int index) {
    int largest = index;

    int left = heap_left_child(index);
    int right = heap_right_child(index);

    if (left < heap.heap_size && heap.array[left] > heap.array[right]) {
        largest = left;
    }

    if (right < heap.heap_size && heap.array[right] > heap.array[largest]) {
        largest = right;
    }

    if (largest != index) {
        exchange(heap.array + largest, heap.array + index);

        max_heapify(heap, largest);
    }

}

void build_max_heap(Heap heap) {
    int i;
    for (i = heap.length / 2 - 1; i >= 0; i--) {
        max_heapify(heap, i);
    }
}

void max_heap_sort(Heap heap) {
    int i;
    build_max_heap(heap);
    for (i = heap.length - 1; i > 1; i--) {
        exchange(heap.array, heap.array + i);

        heap.heap_size -= 1;

        max_heapify(heap, 0);
    }
}
复制代码

 

main.c

复制代码
#import <stdio.h>
#import "heap_sort.h"

int main() {
    int i;
    int array[8] = {6, 5, 3, 1, 8, 7, 2, 4};
    Heap heap = {array, 8, 8};

    max_heap_sort(heap);

    for(i = 0; i < heap.length; i++) {
        printf("%d \n", heap.array[i]);
    }

    return 0;
}
复制代码

 

运行结果:

 

posted on   Anthony Li  阅读(727)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
< 2013年3月 >
24 25 26 27 28 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

博客园博客已停止更新,博客地址:dyinigbleed.com

点击右上角即可分享
微信分享提示