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 ...
随机推荐
- 宏定义(#define)和常量(const)的区别
最近开始准备一边做实验室的研究,一边记录一些遇到的编程中的小知识点.今天在测试对矩阵进行SVD分解时,需要定义矩阵的行和列的大小,我习惯性的用宏定义来定义了这两个变量,在运行的时候,就开始思考宏定义和 ...
- Web服务器(Apache)虚拟主机的配置
一.定义 所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB.FTP.Mail等服务. 二.虚拟主机的实现方法有三种: 基于IP的方法,基于主机名的方法和基于端口的法官法. ...
- oracle检查点checkpoint信息
1.关于checkpoint的概述 checkpoint是oracle在数据库一致性关闭.实例恢复和oracle基本操作中不可缺少的机制,包含以下相关的含义: A.检查点的位置(checkpoint ...
- 【转】JS设计模式开篇
(原文地址:http://blog.chinaunix.net/uid-26672038-id-3904513.html) 本文主要讲述一下,什么是设计模式(Design patter ...
- htmlcss笔记--标签默认值样式重置css reset
1.<a>标签 有默认文字修饰:下划线, 去除:text-decoration:none; text-decoration属性值: none 默认.定义标准的文本. underline 定 ...
- HDU-4720 Naive and Silly Muggles 圆的外心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720 先两两点之间枚举,如果不能找的最小的圆,那么求外心即可.. //STATUS:C++_AC_0M ...
- IOS下arm64汇编疑问
之前所有关于32位下的纯汇编.s代码,在编译arm64的时候,很多错误,不得已只能用C代码. 但是arm_neon.h内部类C接口的汇编,基本上没有问题.不敢完全保证,还有待确认.关于arm64位的汇 ...
- HDU 5805 NanoApe Loves Sequence (模拟)
NanoApe Loves Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5805 Description NanoApe, the ...
- STL学习系列三:Deque容器
1.Deque简介 deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口上和vector非常 ...
- 使用https时,网站一些内容不能正常显示的问题
在网站开发过程中,使用http网站页面一切正常. 但改成https后,发现网站一些页面不能正常显示出来,比如看上去没有样式等. 原因是: 在程序中调用了比如JQuery,而引用的URL使用的是Http ...