CLRS第八章思考题
来源:互联网 发布:sql update 条件更新 编辑:程序博客网 时间:2024/06/13 23:32
思考题8-1
a)
b) 当
证明:设
c) 要证明结论,只需分别证明
证明
证明
因此得证。
d)
因而在
得证。
e)
f) 为了构建和
思考题8-2
a) 计数排序。
b) 快排中的PARTITION
即可实现。
c) 冒泡排序、插入排序都可。
d) 基数排序需要使用稳定的排序方法,因此必须满足条件2。所以只有计数排序可以。
e) 给一个简单的方法。我们先统计每个记录出现的次数,然后直接放入数组中,举例
显然这个是不稳定的。
#include <iostream>#include <cstring>using std::cout;using std::endl;void COUNT_SORT(int *array,int length,int max_int_k){ int *C = new int[max_int_k+1]; memset(C,0,sizeof(int)*(max_int_k+1)); for(int i = 0; i < length; ++i) ++C[array[i]]; int cnt = 0; for(int i = 1; i <= max_int_k; ++i) { while(C[i] > 0) { array[cnt++] = i; --C[i]; } } delete []C;}int main(){ int ia[] = {1,3,2,3,1,1,2,7}; COUNT_SORT(ia,8,7); for(int i = 0; i < 8; ++i) cout << ia[i] << ' '; cout << endl; return 0;}
思考题8-3
a) 首先假设全部是整数并且数字没有前导
设数字位数为
b) 将首字母相同的分成一组,使用计数排序第一个字母;若第一个字母相同,则去掉首字母再分组,递归排序直到只有一个字母。需要注意的是长度为
思考题8-4
a) 很简单,比较每个红色和蓝色水壶,即暴力破解,总时间
b) 类似决策树模型,结点内部是要比较的两个水壶,比较结果引出三条边,红色大于蓝色、等于蓝色、小于蓝色。叶结点就是匹配的水壶。
c)
1.随机选一个红色水壶
2.找出对应的蓝色水壶
3.挑出的红色水壶在和蓝色组水壶对比时,蓝色水壶就可以分成两组(可能一组为空);
4.类似 3,步骤 2 选出的蓝色水壶对比红色组,红色水壶分成两组;
5.分别递归划分的两组水壶。显然期望比较次数
下面的代码用两组数字代表两种颜色不同大小的水壶,最后排序两组数表示配对成功。
#include <iostream>#include <cstdlib>using std::cout;using std::endl;int partition(int *red,int *blue,int p,int r){ int index_red = rand() % (r - p + 1) + p; int index_blue; //找出红色对于的蓝色的下标 for(index_blue = p; index_blue <= r; ++index_blue) { if(red[index_red] == blue[index_blue]) break; } //交换blue的piovt int temp = blue[index_blue]; blue[index_blue] = blue[r]; blue[r] = temp; //划分blue int i = p - 1; int blue_pivot = red[index_red];//蓝色不能和蓝色的水壶比较,所以此处用的红色 for(int j = p; j < r; ++j) { if(blue[j] <= blue_pivot) { ++i; temp = blue[j]; blue[j] = blue[i]; blue[i] = temp; } } temp = blue[r]; blue[r] = blue[i+1]; blue[i+1] = temp; //交换red的piovt temp = red[index_red]; red[index_red] = red[r]; red[r] = temp; //划分red int k = p - 1; int red_piovt = blue[i+1]; for(int j = p; j < r; ++j) { if(red[j] <= red_piovt) { ++k; temp = red[j]; red[j] = red[k]; red[k] = temp; } } temp = red[r]; red[r] = red[k+1]; red[k+1] = temp; //返回划分的下标 return k + 1;}void match_jugs(int *red,int *blue,int p,int r){ if(p < r) { int q = partition(red,blue,p,r); match_jugs(red,blue,p,q-1); match_jugs(red,blue,q+1,r); }}int main(){ int ia[] = {13,-3,-25,20,-16,-23,18}; int ib[] = {18,20,-3,-23,13,-16,-25}; match_jugs(ia,ib,0,6); for(int i = 0; i < 7; ++i) cout << ia[i] << ' '; cout << endl; for(int i = 0; i < 7; ++i) cout << ib[i] << ' '; cout << endl; return 0;}
思考题8-5
a) 就是普通的排序。
b)
c)证明:
d) 由 c) 的证明,可以将数组分成
e) 和练习 6.5-9 十分类似,建一个堆,运行时间是
f) 一共
思考题8-6
a)
b) 首先
化简有:
c) 很明显,不比较怎么知道哪个元素出列。
d) 对元素
思考题8-7
题目出奇的长,实在不想看,附一个链接,答案见此处这里写链接内容
- CLRS第八章思考题
- CLRS第三章思考题
- CLRS第二章思考题
- CLRS第四章思考题
- CLRS第五章思考题
- CLRS 第六章思考题
- CLRS第七章思考题
- CLRS第九章思考题
- CLRS第十一章思考题
- CLRS第十二章思考题
- CLRS第十三章思考题
- CLRS第十四章思考题
- CLRS第十六章思考题
- CLRS第十章思考题
- CLRS 第十七章思考题
- CLRS第十五章思考题1-4
- CLRS第十五章思考题5-8
- CLRS第十五章思考题9-12
- HiHo --#1043 : 完全背包
- 教你做一款高口碑的产品
- 全排列
- Opencv用鼠标绘制填充多边形
- java内存区域
- CLRS第八章思考题
- Jmeter学习笔记(一)
- 设计模式: 自己手动写一个装饰者模式
- Js学习笔记(一)
- angularJS中的ng-click和ng-change
- 关于mysql has gone away的解决办法
- 获取问号后面的参数
- SVN changelist and commit
- vagrant up default: Warning: Connection timeout. Retrying