Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
kunal768 wants to merge16 commits intoabranhe:masterfromkunal768:master
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
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
1 change: 1 addition & 0 deletionsallalgorithms/subarray/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
from .kadane import *
44 changes: 44 additions & 0 deletionsallalgorithms/subarray/kadane.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletiondocs/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsdocs/subarray/kadane.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsreadme.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/test_kadane.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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()

[8]ページ先頭

©2009-2025 Movatter.jp