divide-conquer-combine(4.1 from the introduction to algorithm)
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)的更多相关文章
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- divide&conquer:find max array
package max_subarrayy;import java.lang.Math;public class max_subarrayy { private static int[] array; ...
- leetcode Ch4-Binary Tree & BFS & Divide/Conquer
一. 1. Lowest Common Ancestor class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, ...
- 分治法(divide & conquer)与动态规划(dynamic programming)应用举例
动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...
- 《Introduction to Algorithm》-chaper33-计算几何学
叉积: 在平面中我们为了度量一条直线的倾斜状态,为引入倾斜角这个概念.而通过在直角坐标系中建立tan α = k,我们实现了将几何关系和代数关系的衔接,这其实也是用计算机解决几何问题的一个核心,计算机 ...
- 《Introduction to Algorithm》-chaper30-多项式与快速傅里叶变换
两个n次多项式的相加最直接的方法所需要的时间是O(n),而实现两个n次多项式的乘法的直接方法则需要O(n^2),本章讨论的快速傅里叶变换(FFT),将会将这一过程的时间复杂度降至O(nlogn).同时 ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- Algorithm in Practice - Sorting and Searching
Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisi ...
- Python数据结构与算法设计总结篇
1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static ...
随机推荐
- Spring 定时任务的实现<转>
本人暂时用到的实现定时任务的方式有2种 一.注解方式实现,简单方便 1:在applicationContext.xml中加入下面的配置, 这是spring的组件扫描,保证含有定时任务的类,能被spri ...
- <转>详解DNS的常用记录(下):DNS系列之三
在上篇博文中我们介绍了DNS服务器中几种不可或缺的记录,包括A记录,NS记录和SOA记录.本篇博文中我们将继续为大家介绍DNS的另外几种常用记录,希望能对大家了解DNS有所帮助. 四 MX记录 MX记 ...
- springmvc基础知识
springmvc框架,类似于struts,主要用于MVC的控制层 spring的简单配置(非注解): spring-mvc.xml文件(springMVC框架的基本文件) web.xml文件 ja ...
- python中struct模块及packet和unpacket
转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...
- Apache Hadoop 镜像地址
HTTP http://apache.fayea.com/ http://apache.opencas.org/ http://mirror.bit.edu.cn/apache/ http://mir ...
- Junit3.8 私有方法测试
1. 测试类的私有方法时可以采取两种方式:1) 修改方法的访问修饰符,将private修改为default或public(但不推荐采取这种方式).2) 使用反射在测试类中调用目标类的私有方法(推荐). ...
- Spark RDD概念学习系列之RDD的重要内部属性(十五)
RDD的重要内部属性 通过 RDD 的内部属性,用户可以获取相应的元数据信息.通过这些信息可以支持更复杂的算法或优化. 1)分区列表:通过分区列表可以找到一个 RDD 中包含的所有分区及其所在地址. ...
- Spring Autowiring by AutoDetect
In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...
- 玩转轻巧型C/C++ IDE之C-Free(配置GCC、Visual C++、Borland C++编译器)
玩转轻巧型C/C++ IDE之C-Free(配置GCC.Visual C++.Borland C++编译器) 之前在写一点简单的C/C++代码时习惯了VC++6.0,但是由于在windows7下VC6 ...
- js即时监听文本内容
<script type="text/javascript"> //其他浏览器 function OnInput (event) { alert ("文本内容 ...