mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00:00
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
This commit is contained in:
parent
4531ea425e
commit
ce7faa5a3a
23
other/largest_subarray_sum.py
Normal file
23
other/largest_subarray_sum.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
from sys import maxsize
|
||||
|
||||
|
||||
def max_sub_array_sum(a: list, size: int = 0):
|
||||
"""
|
||||
>>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7])
|
||||
-3
|
||||
"""
|
||||
size = size or len(a)
|
||||
max_so_far = -maxsize - 1
|
||||
max_ending_here = 0
|
||||
for i in range(0, size):
|
||||
max_ending_here = max_ending_here + a[i]
|
||||
if max_so_far < max_ending_here:
|
||||
max_so_far = max_ending_here
|
||||
if max_ending_here < 0:
|
||||
max_ending_here = 0
|
||||
return max_so_far
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
a = [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7]
|
||||
print(("Maximum contiguous sum is", max_sub_array_sum(a, len(a))))
|
|
@ -1,54 +0,0 @@
|
|||
"""
|
||||
Python implementation of bogobogosort, a "sorting algorithm
|
||||
designed not to succeed before the heat death of the universe
|
||||
on any sizable list" - https://en.wikipedia.org/wiki/Bogosort.
|
||||
|
||||
Author: WilliamHYZhang
|
||||
"""
|
||||
|
||||
import random
|
||||
|
||||
|
||||
def bogo_bogo_sort(collection):
|
||||
"""
|
||||
returns the collection sorted in ascending order
|
||||
:param collection: list of comparable items
|
||||
:return: the list sorted in ascending order
|
||||
|
||||
Examples:
|
||||
>>> bogo_bogo_sort([0, 5, 3, 2, 2])
|
||||
[0, 2, 2, 3, 5]
|
||||
>>> bogo_bogo_sort([-2, -5, -45])
|
||||
[-45, -5, -2]
|
||||
>>> bogo_bogo_sort([420, 69])
|
||||
[69, 420]
|
||||
"""
|
||||
|
||||
def is_sorted(collection):
|
||||
if len(collection) == 1:
|
||||
return True
|
||||
|
||||
clone = collection.copy()
|
||||
while True:
|
||||
random.shuffle(clone)
|
||||
ordered = bogo_bogo_sort(clone[:-1])
|
||||
if clone[len(clone) - 1] >= max(ordered):
|
||||
break
|
||||
|
||||
for i in range(len(ordered)):
|
||||
clone[i] = ordered[i]
|
||||
|
||||
for i in range(len(collection)):
|
||||
if clone[i] != collection[i]:
|
||||
return False
|
||||
return True
|
||||
|
||||
while not is_sorted(collection):
|
||||
random.shuffle(collection)
|
||||
return collection
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
user_input = input("Enter numbers separated by a comma:\n").strip()
|
||||
unsorted = [int(item) for item in user_input.split(",")]
|
||||
print(bogo_bogo_sort(unsorted))
|
21
sorts/i_sort.py
Normal file
21
sorts/i_sort.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
def insertionSort(arr):
|
||||
"""
|
||||
>>> a = arr[:]
|
||||
>>> insertionSort(a)
|
||||
>>> a == sorted(a)
|
||||
True
|
||||
"""
|
||||
for i in range(1, len(arr)):
|
||||
key = arr[i]
|
||||
j = i - 1
|
||||
while j >= 0 and key < arr[j]:
|
||||
arr[j + 1] = arr[j]
|
||||
j -= 1
|
||||
arr[j + 1] = key
|
||||
|
||||
|
||||
arr = [12, 11, 13, 5, 6]
|
||||
insertionSort(arr)
|
||||
print("Sorted array is:")
|
||||
for i in range(len(arr)):
|
||||
print("%d" % arr[i])
|
Loading…
Reference in New Issue
Block a user