Skip to content

Commit ce7faa5

Browse files
anubhav-sharma13cclauss
authored andcommitted
Largest subarray sum (#1404)
* Insertion_sort * largest subarray sum * updated print command * removed extraspaces * removed sys.maxint * added explaination * Updated function style * Update largest_subarray_sum.py * Update i_sort.py * Delete bogo_bogo_sort.py
1 parent 4531ea4 commit ce7faa5

File tree

3 files changed

+44
-54
lines changed

3 files changed

+44
-54
lines changed

other/largest_subarray_sum.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from sys import maxsize
2+
3+
4+
def max_sub_array_sum(a: list, size: int = 0):
5+
"""
6+
>>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7])
7+
-3
8+
"""
9+
size = size or len(a)
10+
max_so_far = -maxsize - 1
11+
max_ending_here = 0
12+
for i in range(0, size):
13+
max_ending_here = max_ending_here + a[i]
14+
if max_so_far < max_ending_here:
15+
max_so_far = max_ending_here
16+
if max_ending_here < 0:
17+
max_ending_here = 0
18+
return max_so_far
19+
20+
21+
if __name__ == "__main__":
22+
a = [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7]
23+
print(("Maximum contiguous sum is", max_sub_array_sum(a, len(a))))

sorts/bogo_bogo_sort.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

sorts/i_sort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def insertionSort(arr):
2+
"""
3+
>>> a = arr[:]
4+
>>> insertionSort(a)
5+
>>> a == sorted(a)
6+
True
7+
"""
8+
for i in range(1, len(arr)):
9+
key = arr[i]
10+
j = i - 1
11+
while j >= 0 and key < arr[j]:
12+
arr[j + 1] = arr[j]
13+
j -= 1
14+
arr[j + 1] = key
15+
16+
17+
arr = [12, 11, 13, 5, 6]
18+
insertionSort(arr)
19+
print("Sorted array is:")
20+
for i in range(len(arr)):
21+
print("%d" % arr[i])

0 commit comments

Comments
 (0)