this example is from chapter 4 in 《the introduction to algorithm》

the main idea is all showed in the book , i think maybe realizing the algorithm is a good way to understand it.

/*
from : introduction to algorithm chapter four
algorithm:divide and conquer
the time complexity of this algorithm is O(n * logn) */
#include <stdio.h> int Find_max_crossing_subarray(int *a, int low, int mid, int high)
{
int left_sum = -0xffff;
int sum = ;
for (int i = mid; i >= low; i--)
{
sum += a[i];
if (sum > left_sum)
left_sum = sum;
}
int right_sum = -0xffff;
sum = ;
for (int j = mid+; j <= high; j++)
{
sum += a[j];
if (sum > right_sum)
right_sum = sum;
}
return left_sum + right_sum;
} int find_maximum_subarray(int *a, int low, int high)
{
if (high == low)
{
return *(a+high); // base case: only one element
}
else
{
int mid = (high + low)/;
if ((find_maximum_subarray(a, low, mid) >= find_maximum_subarray(a, mid+, high))&&(find_maximum_subarray(a, low, mid)>=Find_max_crossing_subarray(a,low,mid,high)))
{
return find_maximum_subarray(a, low, mid);
}
else if ((find_maximum_subarray(a, mid+, high) >= find_maximum_subarray(a, low, mid))&&(find_maximum_subarray(a, mid+, high) >= Find_max_crossing_subarray(a,low,mid,high)))
{
return find_maximum_subarray(a, mid+, high);
}
else
{
return Find_max_crossing_subarray(a, low, mid, high);
}
}
} int main()
{
int a[]={,-,-,,-,-,-,,,-,,-,-,,-,};
int b[]={,,,,};
printf("%d\n",find_maximum_subarray(a,,));
printf("%d\n",find_maximum_subarray(b,,));
}

divide-conquer-combine(4.1 from the introduction to algorithm)的更多相关文章

  1. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

  2. divide&conquer:find max array

    package max_subarrayy;import java.lang.Math;public class max_subarrayy { private static int[] array; ...

  3. leetcode Ch4-Binary Tree & BFS & Divide/Conquer

    一. 1. Lowest Common Ancestor class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, ...

  4. 分治法(divide & conquer)与动态规划(dynamic programming)应用举例

    动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...

  5. 《Introduction to Algorithm》-chaper33-计算几何学

    叉积: 在平面中我们为了度量一条直线的倾斜状态,为引入倾斜角这个概念.而通过在直角坐标系中建立tan α = k,我们实现了将几何关系和代数关系的衔接,这其实也是用计算机解决几何问题的一个核心,计算机 ...

  6. 《Introduction to Algorithm》-chaper30-多项式与快速傅里叶变换

    两个n次多项式的相加最直接的方法所需要的时间是O(n),而实现两个n次多项式的乘法的直接方法则需要O(n^2),本章讨论的快速傅里叶变换(FFT),将会将这一过程的时间复杂度降至O(nlogn).同时 ...

  7. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  8. Algorithm in Practice - Sorting and Searching

    Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisi ...

  9. Python数据结构与算法设计总结篇

    1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static ...

随机推荐

  1. 纯css实现扁平化360卫士logo demo

    前几天在w3ctech上看到有人用纯css写出了360卫士的logo,感觉蛮好玩的. 因为自己用css以来,还没有写过这种玩意,出于娱乐,我也来试着尝试一下. 开始也不知到怎么下手,最棘手的是那两个像 ...

  2. Hbase物理模型

    ​Hbase ​ 一种高可靠,面向列,可伸缩,事实读写的分布式数据库. 利用HDFS作为其文件存储系统. MapReduce处理数据. Zookeeper分布式协同服务. 数据结构 Row Key:行 ...

  3. How do I use SOCKS proxy in delphi?

    TCP====== For Indy 8.0 In Delphi, do the following: IdTCPClient1.SocksInfo.Host := [the DNS name of ...

  4. 深入理解jQuery插件开发(转)

    转自:http://blog.jobbole.com/30550/ 如果你看到这篇文章,我确信你毫无疑问会认为jQuery是一个使用简便的库.jQuery可能使用起来很简单,但是它仍然有一些奇怪的地方 ...

  5. Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络(试了,没成功 - -!)

                              A complete click-by-click, step-by-step video of this article is available ...

  6. cocos2d-x 纹理深入研究 第二部分

    转自:http://blog.csdn.net/qq51931373/article/details/9152227 1.纹理控制. 看此代码: CCSprite *pSprite = CCSprit ...

  7. 利用red5搭建一个简单的流媒体直播系统

    http://blog.sina.com.cn/s/blog_51396f890102exmz.html 一.red5安装.设置. 这个过程就不多说了,参见http://blog.csdn.net/l ...

  8. ocp 1Z0-043 61-130题解析

    61. You are working in an online transaction processing (OLTP) environment. You realize that the sal ...

  9. 如何关闭dell inspiron n4010的内置麦克

    如何关闭dell inspiron n4010的内置麦克 dell inspiron n4010这款电脑的内置麦克是默认开启的,如果你的扩音器音量开得稍大,当你打字的时候就会听到回音,最讨厌的是,当你 ...

  10. Simulator模拟器 硬件键盘不能输入

    快捷键: Command + Shift +K