From 9e8b694078e9b2714fb00d4bba8ca4f549cbbb02 Mon Sep 17 00:00:00 2001 From: Abdul Kadir Olia Date: Tue, 1 Oct 2019 20:57:42 -0700 Subject: [PATCH 1/2] Add cataegory divide_and_conquer --- Divide_And_Conquer/max_subarray.py | 37 ++++++++++++++++++++++++++++++ PULL_REQUEST_TEMPLATE.md | 12 ++++------ 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 Divide_And_Conquer/max_subarray.py diff --git a/Divide_And_Conquer/max_subarray.py b/Divide_And_Conquer/max_subarray.py new file mode 100644 index 00000000..52530655 --- /dev/null +++ b/Divide_And_Conquer/max_subarray.py @@ -0,0 +1,37 @@ +def find_max_crossing_subarray(arr, low, mid, high): + left_sum = -1 + sum = 0 + for i in range(mid, 0, -1): + sum += arr[i] + if sum > left_sum: + left_sum = sum + + right_sum = -1 + sum = 0 + for j in range(mid+1, high + 1): + sum += arr[j] + if sum > right_sum: + right_sum = sum + + return (left_sum + right_sum) + +def find_max_subarray(arr, low, high): + + if high == low: + return arr[low] + + else: + mid = (low + high) // 2 + left_sum = find_max_subarray(arr, low, mid) + right_sum = find_max_subarray(arr, mid+1, high) + cross_sum = find_max_crossing_subarray(arr, low, mid, high) + + if left_sum >= right_sum and left_sum >= cross_sum: + return left_sum + elif right_sum >= left_sum and right_sum >= cross_sum: + return right_sum + else: + return cross_sum + +arr = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7] +print(find_max_subarray(arr, 0, len(arr) - 1)) \ No newline at end of file diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index cf3586ca..3c361e4c 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,16 +1,12 @@ -``` + # Description -Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. Include a proper title of the Script that is being entered. +Added a new category Divide and Conquer. Added a new script under the category that finds the subarray which has the maximum sum using the divide and conquer paradigm. Fixes # (issue) ## Type of change -Please delete options that are not relevant. - -- [ ] New Script - [ ] New Category (Is any new category being made to accommodate this script) -- [ ] Bug fix (non-breaking change which fixes an issue) -- [ ] New feature (non-breaking change which adds functionality) -``` + + From 436b3b593d5c4ad7012555f4f2671106b49b0544 Mon Sep 17 00:00:00 2001 From: Abdul Kadir Olia Date: Sat, 5 Oct 2019 11:12:32 -0700 Subject: [PATCH 2/2] Add max_subarray.py script under divide and conquer paradigm --- PULL_REQUEST_TEMPLATE.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 3c361e4c..d3fbde86 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,12 +1,14 @@ - # Description -Added a new category Divide and Conquer. Added a new script under the category that finds the subarray which has the maximum sum using the divide and conquer paradigm. +Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. Include a proper title of the Script that is being entered. Fixes # (issue) ## Type of change -- [ ] New Category (Is any new category being made to accommodate this script) - +Please delete options that are not relevant. +- [ ] New Script +- [ ] New Category (Is any new category being made to accommodate this script) +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) \ No newline at end of file