Skip to content

Commit dd916d4

Browse files
committed
feat: add pyds.maximum_subarray_sum_1
1 parent ed88315 commit dd916d4

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

docs/source/pydatastructs/linear_data_structures/algorithms.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ Algorithms
4545

4646
.. autofunction:: pydatastructs.jump_search
4747

48-
.. autofunction:: pydatastructs.intro_sort
48+
.. autofunction:: pydatastructs.intro_sort
49+
50+
.. autofunction:: pydatastructs.maximum_subarray_sum_1

pydatastructs/linear_data_structures/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
jump_search,
4848
selection_sort,
4949
insertion_sort,
50-
intro_sort
50+
intro_sort,
51+
maximum_subarray_sum_1
5152
)
5253
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
'jump_search',
3131
'selection_sort',
3232
'insertion_sort',
33-
'intro_sort'
33+
'intro_sort',
34+
'maximum_subarray_sum_1'
3435
]
3536

3637
def _merge(array, sl, el, sr, er, end, comp):
@@ -1850,3 +1851,62 @@ def partition(array, lower, upper):
18501851
intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold)
18511852

18521853
return array
1854+
1855+
def maximum_subarray_sum_1(array, **kwargs):
1856+
"""
1857+
Finds the maximum subarray sum of the given array using a brute force approach.
1858+
Parameters
1859+
==========
1860+
array: OneDimensionalArray
1861+
The array for which the maximum subarray sum
1862+
has to be found.
1863+
start: int
1864+
The starting index of the portion
1865+
which is to be considered.
1866+
Optional, by default 0
1867+
end: int
1868+
The ending index of the portion which
1869+
is to be considered.
1870+
Optional, by default the index
1871+
of the last position filled.
1872+
comp: lambda/function
1873+
The comparator which is to be used
1874+
for performing comparisons.
1875+
Optional, by default, less than or
1876+
equal to is used for comparing two
1877+
values.
1878+
backend: pydatastructs.Backend
1879+
The backend to be used.
1880+
Optional, by default, the best available
1881+
backend is used.
1882+
Returns
1883+
=======
1884+
output: int
1885+
The maximum subarray sum.
1886+
Examples
1887+
========
1888+
>>> from pydatastructs import OneDimensionalArray as ODA, maximum_subarray_sum_1
1889+
>>> arr = ODA(int, [-2, 1, -3, 4, -1, 2, 1, -5, 4])
1890+
>>> maximum_subarray_sum_1(arr)
1891+
6
1892+
>>> arr = ODA(int, [1, 2, 3, 4, 5])
1893+
>>> maximum_subarray_sum_1(arr)
1894+
15
1895+
References
1896+
==========
1897+
.. [1] https://en.wikipedia.org/wiki/Maximum_subarray_problem
1898+
"""
1899+
raise_if_backend_is_not_python(
1900+
maximum_subarray_sum_1, kwargs.get('backend', Backend.PYTHON))
1901+
start = kwargs.get('start', 0)
1902+
end = kwargs.get('end', len(array) - 1)
1903+
comp = kwargs.get('comp', lambda u, v: u <= v)
1904+
1905+
max_sum = float('-inf')
1906+
for i in range(start, end + 1):
1907+
curr_sum = 0
1908+
for j in range(i, end + 1):
1909+
curr_sum += array[j]
1910+
max_sum = max(max_sum, curr_sum)
1911+
1912+
return max_sum

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
66
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
77
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
8-
selection_sort, insertion_sort, intro_sort, Backend)
8+
selection_sort, insertion_sort, intro_sort, maximum_subarray_sum_1, Backend)
99

1010
from pydatastructs.utils.raises_util import raises
1111
import random

pydatastructs/utils/tests/test_code_quality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _apis():
174174
pyds.Trie, pyds.TrieNode, pyds.SkipList, pyds.RangeQueryStatic, pyds.RangeQueryDynamic, pyds.SparseTable,
175175
pyds.miscellaneous_data_structures.segment_tree.OneDimensionalArraySegmentTree,
176176
pyds.bubble_sort, pyds.linear_search, pyds.binary_search, pyds.jump_search,
177-
pyds.selection_sort, pyds.insertion_sort, pyds.quick_sort, pyds.intro_sort]
177+
pyds.selection_sort, pyds.insertion_sort, pyds.quick_sort, pyds.intro_sort, pyds.maximum_subarray_sum_1]
178178

179179
def test_public_api():
180180
pyds = pydatastructs

0 commit comments

Comments
 (0)