文档库 最新最全的文档下载
当前位置:文档库 › 算法导论练习题1.2-2

算法导论练习题1.2-2

算法导论练习题1.2-2
算法导论练习题1.2-2

算法导论练习题1.2-2

问题来源

《算法导论》练习题1.2-2

假设我们正比较插入排序于归并排序在相同机器上的实现。对规模为n的输入,插入排序运行8n2步,而归并排序运行64nlgn步。问对哪些n值,插入排序优于归并排序?[1]

注:题中lgn为log2n,书中已注明。

题目分析与解决

步数越少效果越优,因此只需要求出

8n2<64nlgn

中n的最大值即可(最小值从1开始按值代入可以求出n>1),化简上式得到

n<8lgn

此类方程为超越方程,推荐编程求解。

代码部分

//C++编写,不是严谨的算法,但可以得出正确结果

#include

#include

int main()

{

double n = 2;

while (n < 8 * std::log2(n))

n++;

std::cout << "n < " << n - 1 << std::endl;

return 0;

}

结果与分析

n=43

8lgn=43.4101

参考文献

[1](美)科尔曼(Cormen, T. H.)等著;殷建平等译. 算法导论(原书第3版). 北京:机械工业出版社. 2013.6-8.

USC算法导论作业4

CSCI303Homework4 Problem1(6.1-1): What are the minimum and maximum numbers of elements in a heap of height h? Solution1: A heap is a semi-complete binary tree,so the minimum number of elements in a heap of height h is2h,and the maximum number of elements in a heap of height h is2h+1?1. Problem2(6.1-6): Is the sequence 23,17,14,6,13,10,1,5,7,12 a max-heap? Solution2: The sequence is not a max-heap,because7is a child of6,but7>6. Problem3(6.2-1): Using Figure6.2as a model,illustrate the operation of Max-Heapify(A,3)on the array A= 27,17,3,16,13,10,1,5,7,12,4,8,9,10 . Solution3:

Problem4(6.2-6): Show that the worst-case running time of Max-Heapify on a heap of size n is?(lg n).(Hint:For a heap with n nodes,give node values that cause Max-Heapify to be called recursively at every node on a path from the root down to a leaf.) Solution4: Consider a heap of n nodes where the root node has been changed to contain the smallest value of all the nodes.Now when we call Max-Heapify on the root,the value will have to be exchanged down with its child at every level,until it reaches the lowest level.This is because,after every swapping,the value will still be smaller than both its children(since it is the minimum),until it reaches the lowest level where it has no more children.In such a heap,the number of exchanges to max-heapify the root will be equal to the height of the tree,which is lg n.So the worst case running time is?(lg n).

大连理工大学软件学院算法导论第一次大作业源码

\documentclass{ctexart} \usepackage{amsmath} \usepackage{amssymb} \usepackage{fancyhdr} \begin{document} \pagestyle{fancy} \title{算法分析与设计第一次作业} \author{XXXX XXX\XXXXXX} \date{2013/9/11} \maketitle \noindent 3.1-2\ 证明: 证明$(n+a)^b=\Theta(n^b)$等价于证明存在$c_{1},c_{2},n_{0}>0$使得对于任意的 $n>n_{0}$,都有$0\leq c_{1}n^b \leq (n+a)^b \leq c_{2}n^b$成立。 $\because$ $n+a\leq n+|a|$,$\therefore$当$n\geq |a|$时,$n+a\leq 2n$。 又$\because$ $n+a\geq n-|a|$,$\therefore$当$|a|\leq \frac{1}{2}$时,$n+a \geq \frac{1}{2}n$。综上,当$n\geq 2|a|$时,$0\leq \frac{1}{2}n \leq (n+a) \leq 2n$。 $\therefore$ 对于$b>0$,有$0\leq (\frac{1}{2}n)^b \leq (n+a)^b \leq (2n)^b$ $\therefore$ 存在$c_{1} = (\frac{1}{2}n)^b$,$c_{2} = (2n)^b$,$n_{0}=2|a|$, 使得$0\leq c_{1}n^b \leq (n+a)^b \leq c_{2}n^b$成立。\ \ \ $\therefore$原命题得证。 \\ \noindent 3.1-3\ 解释:设运行时间为$F(n)$,则$F(n)\geq 0(n^2)$, $\therefore$ 若$F_{1}(n) = 0(n^2)$,则$F(n)\geq F_{1}(n)$, 又$\because$ $\forall$ n,$T(n)=0$时,$T(n)=0(n^2)$,且运行时间都大于0, $\therefore$对于所有的运行时间$F(n)$都有$F(n)\geq 0(n^2)$, $\therefore$这句话是没有意义的。 \\ \noindent

算法导论第二章答案

第二章算法入门 由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。 给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。 插入排序算法伪代码 INSERTION-SORT(A) 1 for j ← 2 to length[A] 2 do key ←A[j] 3 Insert A[j] into the sorted sequence A[1..j-1] 4 i ←j-1 5 while i > 0 and A[i] > key 6 do A[i+1]←A[i] 7 i ←i ? 1 8 A[i+1]←key C#对揑入排序算法的实现: public static void InsertionSort(T[] Input) where T:IComparable { T key; int i; for (int j = 1; j < Input.Length; j++) { key = Input[j]; i = j - 1; for (; i >= 0 && Input[i].CompareTo(key)>0;i-- ) Input[i + 1] = Input[i]; Input[i+1]=key; } } 揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j] 这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1. 如果按照大部分计算机编程语言的思路,修改为: INSERTION-SORT(A) 1 for j ← 1 to length[A] 2 do key ←A[j] 3 i ←j-1

算法导论

1 第一课课程细节;绪论:算法分析,插入排序法(Insertion Sort),归并排序(Merge Sort) 阅读:1-2章 发测验0 2 演示课1 算法的正确性 发《作业1》 3 第二课渐进记号(Asymptotic Notation)。递归公式(Recurrences):置换法,迭代法,主方法 阅读:3-4 章,除了§4.4 4 第三课分治法:Strassen 算法,费氏数列,多项式乘法。 阅读:28 章第2 节,30章第1节 5 演示课2 递归公式,松散性 阅读:Akra-Bazzi 的讲义 6 第四课快速排序法,随机化算法 阅读:5 章 1 到 3 节,7 章 收《作业1》发《作业2》 7 演示课3 排序法:堆排序,动态集合,优先队列 阅读:6 章 8 第五课线性时间的排序法:时间下界,计数排序法,基数排序法 阅读:8 章第1 到 3 节 收《作业2》发《作业3》 9 第六课顺序统计学,中位数 阅读:9 章 10 演示课4 中位数的应用,桶排序 阅读:8 章第4 节 11 第七课散列,全域散列 阅读:11 章 1 到 3 节 收《作业3》发《作业4》 12 第八课散列函数,完美散列 阅读:11 章第 5 节 13 演示课5 测验1 复习 收《作业4》

14 评分后的作业4可以在中午拿到 15 测验1 16 演示课6 二叉搜索树,树的遍历 阅读:12 章 1 到 3 节 17 第九课二叉搜索树和快速排序法之间的关系;随机二叉搜索树的分析阅读:12 章 4 节 发《作业5》 18 第十课红黑树,旋转,插入,删除 阅读:13 章 19 演示课7 2-3树,B-树 阅读:18 章 1 到 2 节 20 第十一课高级数据结构,动态顺序统计,线段树(区间树) 阅读:14 章 收《作业5》发《作业6》 21 第十二课计算几何,区间查询 阅读:33 章 1 到 2 节 22 演示课8 凸多边形 阅读:33 章 3 节 23 第十三课van Emde Boas树,优先队列 阅读:van Emde Boas 的讲义 收《作业6》发《作业7》 24 第十四课平摊分析,表的复制,可能法 阅读:17 章 25 演示课9 竞争分析,自我排序列 26 第十五课动态规划,最长公共子序列,最优二叉搜索树 阅读:15 章 收《作业7》发《作业8》 27 第十六课贪婪算法,最小生成树 阅读:16 章 1 到 3 节,23 章 28 演示课10 贪婪算法和动态规划的范例

算法导论复习资料

算法导论复习资料 一、选择题:第一章的概念、术语。 二、考点分析: 1、复杂度的渐进表示,复杂度分析。 2、正确性证明。 考点:1)正确性分析(冒泡,归并,选择);2)复杂度分析(渐进表示O,Q,?,替换法证明,先猜想,然后给出递归方程)。 循环不变性的三个性质: 1)初始化:它在循环的第一轮迭代开始之前,应该是正确的; 2)保持:如果在循环的某一次迭代开始之前它是正确的,那么,在下一次迭代开始之前,它也应该保持正确; 3)当循环结束时,不变式给了我们一个有用的性质,它有助于表明算法是正确的。 插入排序算法: INSERTION-SORT(A) 1 for j ← 2 to length[A] 2 do key ←A[j] 3 ?Insert A[j] into the sorted sequence A[1,j - 1]. 4 i ←j - 1 5 while i > 0 and A[i] > key 6 do A[i + 1] ←A[i] 7 i ←i - 1 8 A[i + 1] ←key 插入排序的正确性证明:课本11页。 归并排序算法:课本17页及19页。 归并排序的正确性分析:课本20页。 3、分治法(基本步骤,复杂度分析)。——许多问题都可以递归求解 考点:快速排序,归并排序,渐进排序,例如:12球里面有一个坏球,怎样用最少的次数找出来。(解:共有24种状态,至少称重3次可以找出不同的球) 不是考点:线性时间选择,最接近点对,斯特拉算法求解。 解:基本步骤: 一、分解:将原问题分解成一系列的子问题; 二、解决:递归地解各子问题。若子问题足够小,则直接求解; 三、合并:将子问题的结果合并成原问题的解。 复杂度分析:分分治算法中的递归式是基于基本模式中的三个步骤的,T(n)为一个规模为n的运行时间,得到递归式 T(n)=Q(1) n<=c T(n)=aT(n/b)+D(n)+C(n) n>c 附加习题:请给出一个运行时间为Q(nlgn)的算法,使之能在给定的一个由n个整数构成的集合S 和另一个整数x时,判断出S中是否存在有两个其和等于x的元素。

USC算法导论作业

CSCI 303Homework 1 Problem 1(2.1-1): Using Figure 2.2as a model,illustrate the operation of Insertion-Sort on the array A = 31,41,59,26,41,58 . Solution 1: 265841594131Problem 2(2.2-1): Express the function n 3/1000?100n 2?100n +3in terms of Θ-notation. Solution 2: n 3/1000?100n 2?100n +3=Θ(n 3) Problem 3(Derived from 2.2-2): Consider sorting n numbers stored in array A by ?rst ?nding the smallest element of A and exchanging it with the element in A [1].Then ?nd the second smallest element of A ,and exchange it with A [2].Continue in this manner for the ?rst n ?1elements of A .Write pseudocode for this algorithm,which is known as Selection-Sort .Give the worst-case running times of selection sort in Θ-notation. Solution 3: Selection-Sort (A ) for i ←1to length [A ] do min-value ←A [i ] min-index =i for j =i +1to length [A ] do if A [j ]≤min-value min-value =A [j ] min-index =j A [i ]?A [min-index ] The worst-case running time of Selection-Sort is Θ(n 2).

算法导论习题答案

Chapter2 Getting Start 2.1 Insertion sort 2.1.2 将Insertion-Sort 重写为按非递减顺序排序 2.1.3 计算两个n 位的二进制数组之和 2.2 Analyzing algorithms 当前n-1个元素排好序后,第n 个元素已经是最大的元素了. 最好时间和最坏时间均为2()n Θ 2.3 Designing algorithms 2.3.3 计算递归方程的解 22()2(/2)2,1k if n T n T n n if n for k =?=?+ = >? (1) 当1k =时,2n =,显然有()lg T n n n = (2) 假设当k i =时公式成立,即()lg 2lg 22i i i T n n n i ===?, 则当1k i =+,即12i n +=时, 2.3.4 给出insertion sort 的递归版本的递归式 2.3-6 使用二分查找来替代insertion-sort 中while 循环内的线性扫描,是否可以将算法的时间提高到(lg )n n Θ? 虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但

是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2()n Θ 2.3-7 给出一个算法,使得其能在(lg )n n Θ的时间内找出在一个n 元素的整数数组内,是否存在两个元素之和为x 首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找: Search(A,n,x) QuickSort(A,n); i←1; j←n; while A[i]+A[j]≠x and i,()()b b n a n +=Θ 0a >时,()()2b b b b n a n n n +<+= 对于121,2b c c ==,12()b b b c n n a c n <+< 0a <时,()b b n a n +<

《算法导论2版》课后答案_加强版2

1 算法导论第三次习题课 2008.12.17

2 19.1-1 如果x 是根节点:degree[sibling[x]] > sibling[x] 如果x 不是根节点:degree[sibling[x]] = sibling[x] –119.1-3 略

3 19.2-2 过程略( union 操作) 19.2-3 (1)decrease-key (2)extract-min 过程略 19.2-6 算法思想:找到堆中最小值min ,用min-1代替-∞. [遍历堆中树的根节点]

4 15.1-1 15.1-2 略P195 或P329 15.1-4 f i [j]值只依赖f i [j-1]的值,从而可以从2n 压缩为2个。再加上f*、l*、l i [j]。 Print-station(l, I, j ) //i 为线路,j 为station if j>1 then Print-station(l, l i [j], j-1 ) print “line”I, “station”j;

5 15.2-1 略(见课本) 15.2-2 15.2-4 略 MATRIX-CHAIN-MULTIPLY(A, s, i, j) if j>i x= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j), j) y= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j)+1,j) return MATRIX-MULTIPLY(x, y) else return A i

6 15.3-1 (归纳法)递归调用 枚举15.3-2 没有效果,没有重叠子问题 15.3-4 略 (3)n Ο3/2(4/) n n Θ

算法导论 第三版 第35章 答案 英

Chapter35 Michelle Bodnar,Andrew Lohr April12,2016 Exercise35.1-1 We could select the graph that consists of only two vertices and a single edge between them.Then,the approximation algorithm will always select both of the vertices,whereas the minimum vertex cover is only one vertex.more generally,we could pick our graph to be k edges on a graph with2k vertices so that each connected component only has a single edge.In these examples,we will have that the approximate solution is o?by a factor of two from the exact one. Exercise35.1-2 It is clear that the edges picked in line4form a matching,since we can only pick edges from E ,and the edges in E are precisely those which don’t share an endpoint with any vertex already in C,and hence with any already-picked edge. Moreover,this matching is maximal because the only edges we don’t include are the ones we removed from E .We did this because they shared an endpoint with an edge we already picked,so if we added it to the matching it would no longer be a matching. Exercise35.1-3 We will construct a bipartite graph with V=R∪L.We will try to construct it so that R is uniform,not that R is a vertex cover.However,we will make it so that the heuristic that the professor(professor who?)suggests will cause us to select all the vertices in L,and show that|L|>2|R|. Initially start o?with|R|=n?xed,and L empty.Then,for each i from 2up to n,we do the following.Let k= n i .Select S a subset of the vertices of R of size ki,and so that all the vertices in R?S have a greater or equal degree.Then,we will add k vertices to L,each of degree i,so that the union of their neighborhoods is S.Note that throughout this process,the furthest apart the degrees of the vertices in R can be is1,because each time we are picking the smallest degree vertices and increasing their degrees by1.So,once this has been done for i=n,we can pick a subset of R whose degree is one less than the rest of R(or all of R if the degrees are all equal),and for each vertex in 1

算法导论 第八章答案

8.2-4 :在O(1)的时间内,回答出输入的整数中有多少个落在区间[a...b]内。给出的算法的预处理时间为O(n+k) 算法思想:利用计数排序,由于在计数排序中有一个存储数值个数的临时存储区C[0...k],利用这个数组即可。 #include using namespace std; //通过改编计数排序而来,因为有些部分需要注释掉 void counting_sort(int*&a, int length, int k, int*&b, int*&c); int main() { const int LEN =100; int*a =newint[LEN]; for(int i =0; i < LEN; i++) a[i] = (i -50)*(i -50) +4; int* b =new int[LEN]; const int k =2504; int* c =new int[k +1]; counting_sort(a, LEN, k, b, c); //这里需要注释掉 //for(int i = 0; i < LEN; i++) //cout<>m>>n) { if(m >n) cout<<"区间输入不对"< k && m >0) cout<<"个数为"< k && m <=0) cout<<"个数为"<= 0; i--) { b[c[a[i]] - 1] = a[i]; c[a[i]]--; }*/ } PS:计数排序的总时间为O(k+n),在实践中,如果当k = O(n)时,我们常常采用计数排序,

算法导论 课后题答案

Partial Solutions for Introduction to algorithms second edition Professor: Song You TA: Shao Wen

ACKNOWLEDGEMENT CLASS ONE: JINZI CLASS TWO: LIUHAO, SONGDINMIN, SUNBOSHAN, SUNYANG CLASS FOUR:DONGYANHAO, FANSHENGBO, LULU, XIAODONG, CLASS FIVE:GAOCHEN, WANGXIAOCHUAN, LIUZHENHUA, WANGJIAN, YINGYING CLASS SIX: ZHANGZHAOYU, XUXIAOPENG, PENGYUN, HOULAN CLASS: LIKANG,JIANGZHOU, ANONYMITY The collator of this Answer Set, SHAOWen, takes absolutely no responsibility for the contents. This is merely a vague suggestion to a solution to some of the exercises posed in the book Introduction to algorithms by Cormen, Leiserson and Rivest. It is very likely that there are many errors and that the solutions are wrong. If you have found an error, have a better solution or wish to contribute in some constructive way please send an Email to shao_wen_buaa@https://www.wendangku.net/doc/f216083318.html, It is important that you try hard to solve the exercises on your own. Use this document only as a last resort or to check if your instructor got it all wrong. Have fun with your algorithms and get a satisfactory result in this course. Best regards, SHAOWen

麻省理工学院-算法导论

麻省理工学院-算法导论 关于课本的介绍如下: 本书自第一版出版以来,已经成为世界范围内广泛使用的大学教材和专业人员的标准参考手册。本书全面论述了算法的内容,从一定深度上涵盖了算法的诸多方面,同时其讲授和分析方法又兼顾了各个层次读者的接受能力。各章内容自成体系,可作为独立单元学习。所有算法都用英文和伪码描述,使具备初步编程经验的人也可读懂。全书讲解通俗易懂,且不失深度和数学上的严谨性。第二版增加了新的章节,如算法作用、概率分析与随机算法、线性编程等,几乎对第一版的各个部分都作了大量修订。 学过计算机的都知道,这本书是全世界最权威的算法课程的大学课本了,基本上全世界的名牌大学用的教材都是它。这本书一共四位作者,Thomas H. Cormen,Charles E. Leiserson和Ronald L. Rivest是来自MIT的教授,Clifford Stein是MIT出来的博士,现在哥伦比亚大学做教授,四人姓氏的首字母联在一起即是此书的英文简称(CLRS 2e),其中的第三作者Ronald L. Rivest是RSA算法的老大(算法名字里面的R即是指他),四个超级大牛出的一本书,此书不看人生不能算完整。 再介绍一下课堂录像里面授课的两位MIT的老师,第一位,外表“绝顶聪明”的,是本书的第二作者Charles E. Leiserson,以逻辑严密,风趣幽默享誉MIT。第二位,留着金黄色的络腮胡子和马尾发的酷哥是Erik Demaine,21岁即取得MIT教授资格的天才,1981出生,今年才25岁,业余爱好是俄罗斯方块、演戏、琉璃、折纸、杂耍、魔术和结绳游戏。 另外,附上该书的中文电子版,pdg转pdf格式,中文版翻译自该书的第一版,中文书名没有使用《算法导论》,而使用的是《现代计算机常用数据结构和算法》,1994年出版时没有得到国外的授权,属于“私自翻译出版”,译者是南京大学计算机系的潘金贵。 课程重点 算法导论是麻省理工学院电机工程与计算机科学系“理论计算机科学”集中选修课程的先导科目。课程几乎将所有资料放到线上,包括了完整的课堂讲义和习题。课本“算法导论,第二版”,是由Charles Leiserson 教授副笔。 课程描述 本课程教授高效率算法的设计及分析技巧,并着重在有实用价值的方法上。课程主题包含了:排序、搜寻树、堆积及散列;各个击破法、动态编程、偿还分析、图论算法、最短路径、网络流、计算几何、数字理论性算法;多项式及矩阵的运算;高速缓存技术及并行运算。 一般资讯 讲师: Erik Demaine

算法导论课程作业答案

Introduction to Algorithms Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Handout10 Diagnostic Test Solutions Problem1 Consider the following pseudocode: R OUTINE(n) 1if n=1 2then return1 3else return n+R OUTINE(n?1) (a)Give a one-sentence description of what R OUTINE(n)does.(Remember,don’t guess.) Solution:The routine gives the sum from1to n. (b)Give a precondition for the routine to work correctly. Solution:The value n must be greater than0;otherwise,the routine loops forever. (c)Give a one-sentence description of a faster implementation of the same routine. Solution:Return the value n(n+1)/2. Problem2 Give a short(1–2-sentence)description of each of the following data structures: (a)FIFO queue Solution:A dynamic set where the element removed is always the one that has been in the set for the longest time. (b)Priority queue Solution:A dynamic set where each element has an associated priority value.The element removed is the element with the highest(or lowest)priority.

北京航空航天大学 算法导论提纲与习题解答

Exercises: 3.1-1, 3.1-4, 3.1-5, 3.1-7Problems: 3-2 补充题1:小明在上楼梯时每步可以登一级或两级台阶,若小明要上n节台阶,他能有多少种不同的走法呢?求解这个问题的计算复杂度是什么(用O符号表示)? Exercises: 4.1-1, 4.1-2, 4.1-3, 4.1-5, 4.1-6, 4.2-1, 4.2-3, 4.3-1, 4.3-2, 4.3-3 Problems: 4-1 a, d, g, h; 4-4 d, f, i, j; 4-6; 4-7 补充题2:两个大整数(均小于101000)相乘,请设计一个尽可能有效的算法,并分析其计算复杂度。说明:以十进制的个位整数(0~9)相乘作为基本运算。 补充题3:设计一个尽可能有效的算法,找出数组a[n]的最大和最小值,并分析算法的计算复杂度。 Exercises: 5.1-2, 5.2-1, 5.2-3, 5.2-4, 5.3-3, 5.3-4, 5.3-5

Exercises: 15.1-1, 15.1-2, 15.1-4, 15.1-5 Exercises: 15.2-1, 15.2-2, 15.2-3 Exercises: 15.3-1, 15.3-3, 15.3-4 Exercises: 15.4-1, 15.4-2, 15.4-4, 15.4-5 Exercises: 15.5-1, 15.5-2 Exercises: 16.1-2 Exercises: 16.2-2, 16.2-4, 16.3-2 Exercises: 30.1-2 Exercise: 2.3-1, 2.3-5(编程) Algorithm 3 B INARY -S EARCH (A; v; p; r) Input: A sorted array A and a value v. Output: An index i such that v = A[i] or nil . if p > r and v 6= A[p] then return nil end if j A[b(r - p)=2c] if v = A[j] then return j else if v < A[j] then return B INARY -S EARCH (A; v; p; j) else return B INARY -S EARCH (A; v; j; r) end if 2.3-3 当1k =时,2n =,显然有()lg T n n n = 假设当k i =时公式成立,即()lg 2lg 22i i i T n n n i ===?, 则当1k i =+,即12i n +=时, 111111 ()(2)2(2)222(1)22lg(2)lg i i i i i i i i T n T T i i n n ++++++==+=?+=+== ()l g T n n n ∴ = 2.3-6虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2 ()n Θ , 2.3-7(分析并编程实现) 首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找: Search(A,n,x)

算法导论第三版答案

Solution to Exercise2.2-2 S ELECTION-S ORT.A/ n D A:length for j D1to n 1 smallest D j for i D j C1to n if A?i

2-2Selected Solutions for Chapter2:Getting Started A?low::high contains the value .The initial call to either version should have the parameters A; ;1;n. I TERATIVE-B INARY-S EARCH.A; ;low;high/ while low high mid D b.low C high/=2c if ==A?mid return mid elseif >A?mid low D mid C1 else high D mid 1 return NIL R ECURSIVE-B INARY-S EARCH.A; ;low;high/ if low>high return NIL mid D b.low C high/=2c if ==A?mid return mid elseif >A?mid return R ECURSIVE-B INARY-S EARCH.A; ;mid C1;high/ else return R ECURSIVE-B INARY-S EARCH.A; ;low;mid 1/ Both procedures terminate the search unsuccessfully when the range is empty(i.e., low>high)and terminate it successfully if the value has been found.Based on the comparison of to the middle element in the searched range,the search continues with the range halved.The recurrence for these procedures is therefore T.n/D T.n=2/C?.1/,whose solution is T.n/D?.lg n/.

嵌入式学习之路

目标:达到适应嵌入式应用软件开发、嵌入式系统开发或嵌入式驱动开发的基本素质。采用了目前应用最广泛的软硬件开发平台(Linux和Arm)。 学习步骤如下: 1、Linux 基础 安装Linux操作系统 Linux文件系统 (windows的文件共享) Linux的基本命令及使用 Linux启动过程详解 熟悉Linux服务能够独立安装Linux操作系统 能够熟练使用Linux系统的基本命令 认识Linux系统的常用服务安装Linux操作系统 Linux基本命令实践 设置Linux环境变量 定制Linux的服务 Shell 编程基础使用vi编辑文件 使用Emacs编辑文件 使用其他编辑器 2、Shell 编程基础 Shell简介 认识后台程序 Bash编程熟悉Linux系统下的编辑环境 熟悉Linux下的各种Shell 熟练进行shell编程熟悉vi基本操作 熟悉Emacs的基本操作

比较不同shell的区别 编写一个测试服务器是否连通的shell脚本程序编写一个查看进程是否存在的shell脚本程序编写一个带有循环语句的shell脚本程序 3、Linux 下的 C 编程基础 linux C语言环境概述 Gcc使用方法 Gdb调试技术 Autoconf Automake Makefile 代码优化 熟悉Linux系统下的开发环境 熟悉Gcc编译器 熟悉Makefile规则编写Hello,World程序 使用 make命令编译程序 编写带有一个循环的程序 调试一个有问题的程序 4、嵌入式系统开发基础 嵌入式系统概述 交叉编译 配置TFTP服务 配置NFS服务 下载Bootloader和核

相关文档