(一)经典的算法:
• 2.分治算法:
• 对于规模为n的问题分解成k个规模小的问题他们彼此独立,然后再将 它们合并
• 分治策略的算法设计模式
•Divide_and_Conquer(P)
• {
•if (|P|<=n0 ) return adhoc(P);
•divide P into smaller substances P1,P2,…,Pk;
• for (i=1; i<=k; k++)
•yi=Divide-and-Conquer(Pi) //递归解决Pi
•Return merge(y1,y2,…,yk) //合并子问题
• }
• 二分搜索:
•Public static int binarysearch(int[]data,intbeginindex,intendindex)
•Int beginindex=0;int endindex =n-1;
•While(beginindex<endindex)
•{ int mid =(beginindex+endindex)/2
•If(x==a[mid] return mid;
•If(x>a[mid] beginindex=mid+1;
•Else endindex=mid-1;}
•Return -1
• a[0:n-1] 找出
• 第k小的元素
• 快速排序算法
• 是分治算法的应
• 1.分析最优子结构:
• 2.重叠子问题;
• 题-最长公共子序列:
|
|