日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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; }

?

運行結果:

?

轉載于:https://www.cnblogs.com/dyingbleed/archive/2013/03/04/2941989.html

總結

以上是生活随笔為你收集整理的【CLRS】《算法导论》读书笔记(一):堆排序(Heapsort)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。