CLRS 第六章思考题
来源:互联网 发布:上海软件编程培训机构 编辑:程序博客网 时间:2024/06/11 12:21
思考题6-1
a) 不一样,比如 BUILD_MAX_HEAP
、BUILD_MAX_HEAP'
分别变成:
BUILD_MAX_HEAP
BUILD_MAX_HEAP'
b) 调用MAX_HEAP_INSERT
为 MAX_HEAP_INSERT
将引起HEAP-INCREASE-KEY
一直到根结点。结点
思考题6-2
a) 根是
b)
c) 伪代码如下:
MAX-HEAPIFY(A, i) largest = i for j = 1 to d c = CHILD(i, j) if (c <= A.heap-size && A[c] > A[largest]) largest = c if i != largest exchange A[i] with A[largest ] MAX-HEAPIFY(A, largest )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
时间复杂度
d)伪代码如下:
INCREASE-KEY(A, i, key) if key < A[i] error "new key is smaller than current key" A[i] = key while i > 1 && A[i] > A[PARENT(i)] exchange A[i] with A[PARENT(i)] i = PARENT(i)INSERT(A, key) A.heap-size = A.heap-size + 1 A[A.heap-size] = -∞ INCREASE-KEY(A, A.heap-size, key)
时间复杂度
e) 参见d)。
思考题6-3
a) 最简单的方法就是先排序,然后按照顺序从左到右,从上到下依次填充数字,不够的位置填上无穷大。
b) 由于
c) 首先保存
最坏情况下左上的元素要移到右下,共
d) 和 c) 类似,只是每次插入到
最坏情况下右下的元素要移到左上,共
e) 先建一个
f) 从左下角开始,将关键字和当前值比较,
最坏情况从左下到右上,复杂度
实际代码如下:
#include <iostream>#include <limits>using std::cin;using std::cout;using std::endl;void init_Young(int **array,int m,int n){ for(int i = 0; i < m; ++i) for(int j = 0; j < n; ++j) array[i][j] = INT_MAX;}void Youngify(int **array,int m,int n,int i,int j) //数组m行n列,调整的元素位置i行j列{ while(1) { int i_1 = i, j_1 = j; if(i + 1 < m && array[i][j] > array[i+1][j]) i_1 = i + 1; if(j + 1 < n && array[i][j] > array[i][j+1]) j_1 = j + 1; if(i_1 != i || j_1 != j) { if(array[i_1][j] < array[i][j_1]) //array[i][j]和其中较小的数交换 j_1 = j; else i_1 = i; int temp = array[i][j]; array[i][j] = array[i_1][j_1]; array[i_1][j_1] = temp; i = i_1; j = j_1; } else break; }}int Young_EXTRACT_MIN(int **array,int m,int n){ int smallest = array[0][0]; array[0][0] = array[m-1][n-1]; array[m-1][n-1] = INT_MAX; Youngify(array,m,n,0,0); return smallest;}void Young_DECREASE_KEY(int **array,int m,int n,int i,int j,int key){ if(array[i][j] < key) return; array[i][j] = key; while(1) { int i_1 = i, j_1 = j; if(i - 1 >= 0 && array[i][j] < array[i-1][j]) i_1 = i - 1; if(j - 1 >= 0 && array[i][j] < array[i][j-1]) j_1 = j - 1; if(i_1 != i || j_1 != j) { if(array[i_1][j] > array[i][j_1]) //array[i][j]和其中较大的数交换 j_1 = j; else i_1 = i; int temp = array[i][j]; array[i][j] = array[i_1][j_1]; array[i_1][j_1] = temp; i = i_1; j = j_1; } else break; }}void Young_INSERT(int **array,int m,int n,int key){ Young_DECREASE_KEY(array,m,n,m-1,n-1,key);}void Young_SORT(int **Young,int m,int n,int **array){ for(int i = 0; i < m; ++i) for(int j = 0; j < n; ++j) Young_INSERT(Young,m,n,array[i][j]);}bool Young_FIND_ELEMENT(int **Young,int m,int n,int key){ int i = m - 1, j = 0; while(i >= 0 && j < n) { if(Young[i][j] < key) ++j; else if(Young[i][j] > key) --i; else return true; } return false;}int main(){ int **Young = new int *[2]; for(int i = 0; i < 2; ++i) Young[i] = new int[3]; init_Young(Young,2,3); int **array = new int *[2]; for(int i = 0; i < 2; ++i) array[i] = new int[3]; for(int i = 0; i < 2; ++i) { for(int j = 0; j < 3; ++j) { int key; cin >> key; array[i][j] = key; } } Young_SORT(Young,2,3,array); int value; cout << "Enter the key you want to find:"; cin >> value; if(Young_FIND_ELEMENT(Young,2,3,value)) cout << "yes" << endl; else cout << "no" << endl; cout << Young_EXTRACT_MIN(Young,2,3); for(int i = 0; i < 2; ++i) { delete []Young[i]; delete []array[i]; } delete []Young; delete []array; return 0;}
- CLRS 第六章思考题
- CLRS第三章思考题
- CLRS第二章思考题
- CLRS第四章思考题
- CLRS第五章思考题
- CLRS第七章思考题
- CLRS第八章思考题
- CLRS第九章思考题
- CLRS第十一章思考题
- CLRS第十二章思考题
- CLRS第十三章思考题
- CLRS第十四章思考题
- CLRS第十六章思考题
- CLRS第十章思考题
- CLRS 第十七章思考题
- CLRS第十五章思考题1-4
- CLRS第十五章思考题5-8
- CLRS第十五章思考题9-12
- ZOJ-2587 Unique Attack(最小割是否唯一判定)
- 贪心之多机调度问题
- ArcGIS教程:分水岭
- Keystone初探
- Intellij IDEA 14.1.4 Scala开发环境搭建
- CLRS 第六章思考题
- aauto学习系列之<7>控制语句2
- java TreeMap 排序 与 TreeSet 排序
- ios开发之视图控制器(UIViewController)-- 详解
- 旁观者效应
- 如何在QML应用中创建一个Context Menu
- 黑马程序员—面向对象
- hibernate持久化对象的三个状态
- sql跨数据库批量插入