我是正常蛇

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Steps:

  • Divide the problem (instance) into subproblems.
  • Conquer the subproblems by solving them recursively.
  • Combine subproblem solutions.

A typical algorithm solved by D&V is merge sort. Frist we partitions the array into two sorted array recursivly, that is the Dvide part.Then we combine the two sorted array into one sorted array(the subroutine), that is the conquer part. At last we combine these steps , that is the combine part (almost do nothing, that's why the name got no "combine").

 

A example of D&C solution. compute xn  .  T(n) = lgn (write by myself don't judge pl)

 1 function ntox(x,n)
 2 {
 3     if(n == 1) return x;
 4     if(n%2 == 0)
 5     {//even
 6         var res = ntox(x,n/2);
 7         var result = res * res;
 8     }
 9     else
10     {//odd
11         var res = ntox(x,(n-1)/2) ;
12         var result = res * res * x;
13     }
14     return result;
15 }
posted on 2012-09-02 20:49  我是正常蛇  阅读(216)  评论(0编辑  收藏  举报