Skip to content

Commit 860e008

Browse files
author
yuvraj-rathod-1202
committed
write algorithm maximum_subarray_sum in algorithm.py
1 parent ed88315 commit 860e008

File tree

7 files changed

+71
-2
lines changed

7 files changed

+71
-2
lines changed

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,
5152
)
5253
__all__.extend(algorithms.__all__)
Binary file not shown.
Binary file not shown.

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 69 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',
3435
]
3536

3637
def _merge(array, sl, el, sr, er, end, comp):
@@ -1850,3 +1851,70 @@ 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+
1856+
def maximum_subarray_sum(array, **kwargs):
1857+
"""
1858+
Finds the maximum subarray sum of the given array.
1859+
1860+
Parameters
1861+
==========
1862+
1863+
array: OneDimensionalArray
1864+
The array for which the maximum subarray sum
1865+
has to be found.
1866+
start: int
1867+
The starting index of the portion
1868+
which is to be considered.
1869+
Optional, by default 0
1870+
end: int
1871+
The ending index of the portion which
1872+
is to be considered.
1873+
Optional, by default the index
1874+
of the last position filled.
1875+
comp: lambda/function
1876+
The comparator which is to be used
1877+
for performing comparisons.
1878+
Optional, by default, less than or
1879+
equal to is used for comparing two
1880+
values.
1881+
backend: pydatastructs.Backend
1882+
The backend to be used.
1883+
Optional, by default, the best available
1884+
backend is used.
1885+
1886+
Returns
1887+
=======
1888+
1889+
output: int
1890+
The maximum subarray sum.
1891+
1892+
Examples
1893+
========
1894+
1895+
>>> from pydatastructs import OneDimensionalArray as ODA, maximum_subarray_sum
1896+
>>> arr = ODA(int, [-2, 1, -3, 4, -1, 2, 1, -5, 4])
1897+
>>> maximum_subarray_sum(arr)
1898+
6
1899+
>>> arr = ODA(int, [1, 2, 3, 4, 5])
1900+
>>> maximum_subarray_sum(arr)
1901+
15
1902+
1903+
References
1904+
==========
1905+
1906+
.. [1] https://en.wikipedia.org/wiki/Maximum_subarray_problem
1907+
"""
1908+
raise_if_backend_is_not_python(
1909+
maximum_subarray_sum, kwargs.get('backend', Backend.PYTHON))
1910+
start = kwargs.get('start', 0)
1911+
end = kwargs.get('end', len(array) - 1)
1912+
comp = kwargs.get('comp', lambda u, v: u <= v)
1913+
1914+
max_sum = array[start]
1915+
max_ending_here = array[start]
1916+
for i in range(start + 1, end + 1):
1917+
max_ending_here = max(array[i], max_ending_here + array[i])
1918+
max_sum = max(max_sum, max_ending_here)
1919+
1920+
return max_sum
Binary file not shown.
88.5 KB
Binary file not shown.
12.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)