diff --git a/docs/source/pydatastructs/linear_data_structures/algorithms.rst b/docs/source/pydatastructs/linear_data_structures/algorithms.rst index 7ab521cfd..073cc28cc 100644 --- a/docs/source/pydatastructs/linear_data_structures/algorithms.rst +++ b/docs/source/pydatastructs/linear_data_structures/algorithms.rst @@ -45,4 +45,6 @@ Algorithms .. autofunction:: pydatastructs.jump_search -.. autofunction:: pydatastructs.intro_sort \ No newline at end of file +.. autofunction:: pydatastructs.intro_sort + +.. autofunction:: pydatastructs.maximum_subarray_sum \ No newline at end of file diff --git a/pydatastructs/linear_data_structures/__init__.py b/pydatastructs/linear_data_structures/__init__.py index 057adc169..531a8c940 100644 --- a/pydatastructs/linear_data_structures/__init__.py +++ b/pydatastructs/linear_data_structures/__init__.py @@ -47,6 +47,7 @@ jump_search, selection_sort, insertion_sort, - intro_sort + intro_sort, + maximum_subarray_sum, ) __all__.extend(algorithms.__all__) diff --git a/pydatastructs/linear_data_structures/algorithms.py b/pydatastructs/linear_data_structures/algorithms.py index 792bce855..f76c4696f 100644 --- a/pydatastructs/linear_data_structures/algorithms.py +++ b/pydatastructs/linear_data_structures/algorithms.py @@ -30,7 +30,8 @@ 'jump_search', 'selection_sort', 'insertion_sort', - 'intro_sort' + 'intro_sort', + 'maximum_subarray_sum', ] def _merge(array, sl, el, sr, er, end, comp): @@ -1850,3 +1851,70 @@ def partition(array, lower, upper): intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold) return array + + +def maximum_subarray_sum(array, **kwargs): + """ + Finds the maximum subarray sum of the given array. + + Parameters + ========== + + array: OneDimensionalArray + The array for which the maximum subarray sum + has to be found. + start: int + The starting index of the portion + which is to be considered. + Optional, by default 0 + end: int + The ending index of the portion which + is to be considered. + Optional, by default the index + of the last position filled. + comp: lambda/function + The comparator which is to be used + for performing comparisons. + Optional, by default, less than or + equal to is used for comparing two + values. + backend: pydatastructs.Backend + The backend to be used. + Optional, by default, the best available + backend is used. + + Returns + ======= + + output: int + The maximum subarray sum. + + Examples + ======== + + >>> from pydatastructs import OneDimensionalArray as ODA, maximum_subarray_sum + >>> arr = ODA(int, [-2, 1, -3, 4, -1, 2, 1, -5, 4]) + >>> maximum_subarray_sum(arr) + 6 + >>> arr = ODA(int, [1, 2, 3, 4, 5]) + >>> maximum_subarray_sum(arr) + 15 + + References + ========== + + .. [1] https://en.wikipedia.org/wiki/Maximum_subarray_problem + """ + raise_if_backend_is_not_python( + maximum_subarray_sum, kwargs.get('backend', Backend.PYTHON)) + start = kwargs.get('start', 0) + end = kwargs.get('end', len(array) - 1) + comp = kwargs.get('comp', lambda u, v: u <= v) + + max_sum = array[start] + max_ending_here = array[start] + for i in range(start + 1, end + 1): + max_ending_here = max(array[i], max_ending_here + array[i]) + max_sum = max(max_sum, max_ending_here) + + return max_sum diff --git a/pydatastructs/linear_data_structures/tests/test_algorithms.py b/pydatastructs/linear_data_structures/tests/test_algorithms.py index 46609544b..52b570db2 100644 --- a/pydatastructs/linear_data_structures/tests/test_algorithms.py +++ b/pydatastructs/linear_data_structures/tests/test_algorithms.py @@ -5,7 +5,7 @@ cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered, upper_bound, lower_bound, longest_increasing_subsequence, next_permutation, prev_permutation, bubble_sort, linear_search, binary_search, jump_search, - selection_sort, insertion_sort, intro_sort, Backend) + selection_sort, insertion_sort, intro_sort, maximum_subarray_sum, Backend) from pydatastructs.utils.raises_util import raises import random diff --git a/pydatastructs/utils/_backend/cpp/_nodes.cp312-win_amd64.pyd b/pydatastructs/utils/_backend/cpp/_nodes.cp312-win_amd64.pyd new file mode 100644 index 000000000..e68952ccb Binary files /dev/null and b/pydatastructs/utils/_backend/cpp/_nodes.cp312-win_amd64.pyd differ diff --git a/pydatastructs/utils/tests/test_code_quality.py b/pydatastructs/utils/tests/test_code_quality.py index eafa80be1..215d453f2 100644 --- a/pydatastructs/utils/tests/test_code_quality.py +++ b/pydatastructs/utils/tests/test_code_quality.py @@ -174,7 +174,7 @@ def _apis(): pyds.Trie, pyds.TrieNode, pyds.SkipList, pyds.RangeQueryStatic, pyds.RangeQueryDynamic, pyds.SparseTable, pyds.miscellaneous_data_structures.segment_tree.OneDimensionalArraySegmentTree, pyds.bubble_sort, pyds.linear_search, pyds.binary_search, pyds.jump_search, - pyds.selection_sort, pyds.insertion_sort, pyds.quick_sort, pyds.intro_sort] + pyds.selection_sort, pyds.insertion_sort, pyds.quick_sort, pyds.intro_sort, pyds.maximum_subarray_sum] def test_public_api(): pyds = pydatastructs