divide-conquer-combine(4.1 from the introduction to algorithm)
2014-09-22 20:20
357 查看
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.
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 = 0; for (int i = mid; i >= low; i--) { sum += a[i]; if (sum > left_sum) left_sum = sum; } int right_sum = -0xffff; sum = 0; for (int j = mid+1; 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)/2; if ((find_maximum_subarray(a, low, mid) >= find_maximum_subarray(a, mid+1, 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+1, high) >= find_maximum_subarray(a, low, mid))&&(find_maximum_subarray(a, mid+1, high) >= Find_max_crossing_subarray(a,low,mid,high))) { return find_maximum_subarray(a, mid+1, high); } else { return Find_max_crossing_subarray(a, low, mid, high); } } } int main() { int a[16]={13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7}; int b[6]={1,2,3,4,5}; printf("%d\n",find_maximum_subarray(a,0,15)); printf("%d\n",find_maximum_subarray(b,0,5)); }
相关文章推荐
- 《Computer Networking - Protocols in Aciton》读书笔记之Section 4.1: Joining the Internet: Introduction to IP and DHCP
- Introduction to " Divide and Conquer"
- Introduction to the SQL Server Analysis Services Logistic Regression Data Mining Algorithm
- An Introduction to Thread in the upcoming book From: Introduction to the C++ Boost Libraries
- Introduction to Algorithm - Summary of Chapter 1 - The Role of Algorithm in Computing
- How To Implement The Decision Tree Algorithm From Scratch In Python (从零开始在Python中实现决策树算法)
- A brief introduction to XenBus from the perspective of the driver
- 4. Divide-and-Conquer——Introduction to Algorithms Third Edition
- A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning
- Introduction to Using the XML DOM from Visual C++
- Introduction to Using the XML DOM from Visual C++
- Beyond the C++ Standard Library: An Introduction to Boost
- Unable to get the project ile from the web server错误的解决方法
- The principles of service-orientation part 1 of 6: Introduction to service-orientation [by Thomas Erl]
- Introduction to the Personal Software Process——个人软件过程
- ASP.NET makes uploading files from the client to the server a snap(UploadInterface.PostedFile.SaveAs)
- How do i prevent end users from connecting to the database other than my application?", version 8.1.5
- The ultimate Managed DirectSound 9 Tutorial. Part 1: a full introduction to Playback
- Introduction to the Transport Device Interface
- [导入]Unable to get the project ile from the web server错误的解决方法