Skip to content

Maximum subarray sum 1 #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ Algorithms

.. autofunction:: pydatastructs.jump_search

.. autofunction:: pydatastructs.intro_sort
.. autofunction:: pydatastructs.intro_sort

.. autofunction:: pydatastructs.maximum_subarray_sum
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
jump_search,
selection_sort,
insertion_sort,
intro_sort
intro_sort,
maximum_subarray_sum,
)
__all__.extend(algorithms.__all__)
70 changes: 69 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion pydatastructs/utils/tests/test_code_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading