Skip to content

Kadane's algorithm for maximum sum contiguous subarray #39

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 16 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions allalgorithms/subarray/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .kadane import *
44 changes: 44 additions & 0 deletions allalgorithms/subarray/kadane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: UTF-8 -*-
#
# Kadane's Algorithm
# The All ▲lgorithms library for python
#
# Contributed by: Kunal Keshav Singh Sahni
# Github: @kunal768
#

import sys

def maxsum_subarray(arr):
curr = arr[0]
maxx = arr[0]
n = len(arr)

for i in range(1,n):
curr = max(arr[i],curr+arr[i])
maxx = max(curr,maxx)

return maxx


def returnArray(arr):
size = len(arr)
max_so_far = -sys.maxsize - 1
max_ending_here = 0
start = 0
end = 0
s = 0
for i in range(0,size):

max_ending_here += arr[i]

if max_so_far < max_ending_here:
max_so_far = max_ending_here
start = s
end = i

if max_ending_here < 0:
max_ending_here = 0
s = i+1

return [max_so_far,arr[start:end+1]]
7 changes: 6 additions & 1 deletion docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ print(binary_search(arr, 3))
- [Merge Sort](https://python.allalgorithms.com/sorting/merge-sort)
- [Pigeonhole Sort](https://python.allalgorithms.com/sorting/pigeonhole-sort)
- [Selection Sort](https://python.allalgorithms.com/sorting/selection-sort)
- [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort)
- [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sor

- ### String
- [Palindrome Check](https://python.allalgorithms.com/string/palindrom-check)

- ### Subarray
- [Kadane's Algorithm](https://github.com/kunal768/allalgorithms-python/blob/master/allalgorithms/subarray/kadane.py)


# Related

- [allalgorithms-javascript](https://github.com/abranhe/allalgorithms-javascript): All ▲lgorithms Javascript library
Expand Down
36 changes: 36 additions & 0 deletions docs/subarray/kadane.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Kadane's Algorithm

Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this) Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far
## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.subarray import kadane

arr = [-2, 1, 2, 7, 10, 77]

print(kadane.maxsum_subarray(arr))
# -> 97

print(kadane.returnArray(arr))
# -> [97, [1, 2, 7, 10, 77]]
```

## API

### maxsum_subarray(array)

> Return sum of maximum contigious subarray

### returnArray(array)

> Return sum along with the respective subarray

##### Params:

- `array`: Array (may contain negative elements)
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ print(binary_search(arr, 3))
- [Pigeonhole Sort](https://python.allalgorithms.com/sorting/pigeonhole-sort)
- [Selection Sort](https://python.allalgorithms.com/sorting/selection-sort)
- [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort)

- ### String
- [Palindrome Check](https://python.allalgorithms.com/string/palindrom-check)

- ### Subarray
- [Kadane's Algorithm](https://python.allalgorithms.com/subarray/kadane)


# Related

- [allalgorithms-javascript](https://github.com/abranhe/allalgorithms-javascript): All ▲lgorithms Javascript library
Expand Down
19 changes: 19 additions & 0 deletions tests/test_kadane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest

from allalgorithms.subarray import kadane

class TestSearches(unittest.TestCase):
def test_returnArray(self):
self.assertEqual( [11,[2,3,-1,7]], kadane.returnArray([2,3,-1,7]))
self.assertEqual([5,[2,3]], kadane.returnArray([2,3,-2,4]))
self.assertEqual([0, [0]], kadane.returnArray([-1,-1,-0,0]))
self.assertEqual([-1, [-1]], kadane.returnArray([-1]))

def test_maxsum_subarray(self):
self.assertEqual(11,kadane.maxsum_subarray([2,3,-1,7]))
self.assertEqual(5, kadane.maxsum_subarray([2,3,-2,4]))
self.assertEqual(0, kadane.maxsum_subarray([-1,-1,-0,0]))
self.assertEqual(-1, kadane.maxsum_subarray([-1]))

if __name__ == '__main__':
unittest.main()