An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
2.1 递归的概念 • 例6 Hanoi塔问题 • 设a,b,c是3个塔座。开始时,在塔座a上有一叠共n个圆盘,这些圆盘自下而上,由大到小地叠在一起。各圆盘从小到大编号为1,2,…,n,现要求将塔座a上的这一叠圆盘移到塔座b上,并仍按同样顺序叠置。在移动圆盘时应遵守以下移动规则: • 每次只能移动1个圆盘; • 任何时刻都不允许将较大的圆盘压在较小的圆盘之上; • 在满足移动规则1和2的前提下,可将圆盘移至a,b,c中任一塔座上。 • public static void hanoi(int n, int a, int b, int c) { if (n > 0) { hanoi(n-1, a, c, b); move(a,b); hanoi(n-1, c, b, a); } } • 思考:如果塔的个数变为a,b,c,d四个,现要将n个圆盘从a全部移动到d,移动规则不变,求移动步数最小的方案。
算法及其复杂性 • 据此容易设计出二分搜索算法: public static int binarySearch(int [] a, int x, int n) { // 在 a[0] <= a[1] <= ... <= a[n-1]中搜索 x // 找到x时返回其在数组中的位置,否则返回-1 int left = 0; int right = n - 1; while (left <= right) { int middle = (left + right)/2; //注意要取整!!! if (x == a[middle]) return middle; if (x > a[middle]) left = middle + 1; else right = middle - 1; } return -1; // 未找到x } • 算法复杂度分析:每执行一次算法的while循环, 待搜索数组的大小减少一半。最坏情况任意二个相邻元素都要分划到,即2m=n,log2n=m因此,,while循环被执行了O(logn) 次。循环体内运算需要O(1) 时间,因此整个算法在最坏情况下的计算时间复杂性为O(logn) 。
习题2-2/p35 Template (class Type) Int BinarySearch2(Type a[],const Type& x,int n) { Int left=0;int right=n-1; While (left<right-1) { int middle=(left+right)/2; if (x<a[middle]) right = middle; else left=middle; } if (x==a[left]) return left; else return -1; } Template (class Type) Int BinarySearch1(Type a[],const Type& x,int n) { Int left=0;int right=n-1; While (left<=right) { int middle=(left+right)/2; if (x==a[middle]) return middle; if (x>a[middle]) left = middle; else right=middle; } return -1; } 4. 与正确算法BinarySearch5相比,数组段左、右游标left和right的调整不正确,导致陷入死循环。 1. 与主教材中的算法BinarySearch相比,数组段左、右游标left和right的调整不正确,导致陷入死循环。 Template (class Type) Int BinarySearch4(Type a[],const Type& x,int n) { If (n>0 && x>=a[0]) { Int left=0;int right=n-1; While (left<right) { int middle=(left+right)/2; if (x<a[middle]) right = middle-1; else left=middle; } if (x==a[left]) return left; } return -1;} 2. 与主教材中的算法BinarySearch相比,数组段左、右游标left和right的调整不正确,导致当x=a[n-1]时返回错误。 Template (class Type) Int BinarySearch3(Type a[],const Type& x,int n) { Int left=0;int right=n-1; While (left+1!=right) { int middle=(left+right)/2; if (x>=a[middle]) left = middle; else right=middle; } if (x==a[left]) return left; else return -1;} 3. 与正确算法BinarySearch5相比,数组段左、右游标left和right的调整不正确,导致当x=a[n-1]时返回错误。
n/2位 n/2位 n/2位 n/2位 X= Y= A B C D 2.4 大整数的乘法 • 设计一个有效的算法,可以进行两个n位大整数的乘法运算。分成二段再相乘,即分治也 • 小学的方法:O(n2) 效率太低 • 分治法: • X=a2n/2+b • Y=c2n/2+d • XY=ac2n+(ad+bc)2n/2+bd • 复杂度分析 • T(n)=O(n2) 没有改进
void CB2(size) {var t=1; if (size<4) {return ;} var sizesub=size/2; //子块的大小 var sumTmp=0;//判断子块是否有特殊方格 while (sizesub>=2) { // i,j为子块的左上角 for (i=0;i<size;i=i+sizesub*2) { //的坐标 for (j=0;j<size;j=j+sizesub*2) { //左上角子棋盘有特殊方格吗? t++; sumTmp=0; for (dr1=0;dr1<sizesub;dr1++){ for (dc1=0;dc1<sizesub;dc1++) {sumTmp+=Board[dr1+i][dc1+j];} } if (sumTmp==0) { Board[i+sizesub-1][j+sizesub-1]=t; } //右上角子棋盘有特殊方格吗? sumTmp=0; for (dr1=0;dr1<sizesub;dr1++) { for (dc1=0;dc1<sizesub;dc1++) {sumTmp+=Board[dr1+i][dc1+sizesub+j];}} if (sumTmp==0) { Board[i+sizesub-1][j+sizesub]=t;} //左下角子棋盘有特殊方格吗? sumTmp=0; for (dr1=0;dr1<sizesub;dr1++) { for (dc1=0;dc1<sizesub;dc1++) {sumTmp+=Board[dr1+sizesub+i][dc1+j];} } if (sumTmp==0) {Board[i+sizesub][j+sizesub-1]=t; } //右下角有特殊方格吗? sumTmp=0; for (dr1=0;dr1<sizesub;dr1++) { for (dc1=0;dc1<sizesub;dc1++) {sumTmp+=Board[dr1+sizesub+i][dc1+sizesub+j];} } if (sumTmp==0) {Board[i+sizesub][j+sizesub]=t; }} } sizesub=sizesub/2;//可用>>1 } 格子初值为0,首个特方格为1。一次处理4小块即横(i)2*竖(j)2,故步长为i=i+ sizesub*2
//最后都是2行2列的未处理完 for (i=0;i<size;i=i+2) { for (j=0;j<size;j=j+2) { //处理一个小四块 for (dr1=0;dr1<2;dr1++) { for (dc1=0;dc1<2;dc1++) { if (Board[dr1+i][dc1+j]==0) {Board[dr1+i][dc1+j]=t; } } } t++; } } } 非递归算法也可以解决,关键在于细心的观察! 算法描述